loading...

Translation messages are obtained with base::gettext() or base::ngettext(). But, there is no way to specify that one needs translated messages in a different language than the current one in R. These functions have an additional lang= argument that allows to do so. If the lang= argument is not provided in the call, they behave exactly like the base functions.

gettext_(
  ...,
  domain = NULL,
  trim = TRUE,
  lang = getOption("data.io_lang", default = Sys.getenv("LANGUAGE", unset = "en"))
)

gettextf_(
  fmt,
  ...,
  domain = NULL,
  trim = TRUE,
  lang = getOption("data.io_lang", default = Sys.getenv("LANGUAGE", unset = "en"))
)

ngettext_(
  n,
  msg1,
  msg2,
  domain = NULL,
  lang = getOption("data.io_lang", default = Sys.getenv("LANGUAGE", unset = "en"))
)

test_gettext_lang(
  lang = getOption("data.io_lang", default = Sys.getenv("LANGUAGE", unset = "en")),
  n = 1
)

Arguments

...

one of more character vectors.

domain

the 'domain' for the translation, a character string or NULL; see base::gettext() for more details.

trim

logical indicating if the white space trimming should happen.

lang

the target language (usually two lowercase letters, e.g., "en" for English, "fr" for French, "de" for German, etc.)

fmt

a character vector of format strings, each of up to 8192 bytes.

n

a non-negative integer.

msg1

the message to be used in English for n = 1.

msg2

the message to be used in English for n = 0, 2, 3, ....

Value

A character vector with translated messages. test_gettext_lang() only serve to test and demonstrate the translation in a given language.

Details

To prepare your package for translation with these functions, you should import gettext_(), gettextf_() and ngettext_() from svMisc. Then, you define gettext <- gettext_, gettextf <- gettextf_ and ngettext <- ngettext_ in your R scripts in the package. Finally, you change the current directory of your R console to the base folder of the sources of your package and you issue tools::update_pkg_po(".") in R. Then, you create translations for different languages and you provide translated strings with, say, poEdit.

Examples

old_lang <- Sys.setLanguage("fr") # Switch to French for R language

# R look for messages to be translated into gettext() calls, not gettext_()
# So, rename accordingly in your package:
gettext <- svMisc::gettext_
gettextf <- svMisc::gettextf_
ngettext <- svMisc::ngettext_

# Retrieve strings in same language
gettext("empty model supplied", "incompatible dimensions",
 domain="R-stats", lang = "fr")
#> [1] "empty model supplied"    "incompatible dimensions"

# Retrieve strings in different languages
gettext("empty model supplied", "incompatible dimensions",
  domain="R-stats", lang = "en")
#> [1] "empty model supplied"    "incompatible dimensions"
gettext("empty model supplied", "incompatible dimensions",
  domain="R-stats", lang = "de")
#> [1] "empty model supplied"    "incompatible dimensions"

# Try to get strings translated in an unknown language (just return the strings)
gettext("empty model supplied", "incompatible dimensions",
  domain="R-stats", lang = "xx")
#> [1] "empty model supplied"    "incompatible dimensions"

# Test with some translations from the svMisc package itself:
svMisc::test_gettext_lang()
#> Test of svMisc's `gettext()` and `gettextf()`:
#> This should be transtlated, if 'fr' language is supported.
#> This is message number 3
#> You asked for only one item
svMisc::test_gettext_lang("fr", n = 1)
#> Test of svMisc's `gettext()` and `gettextf()`:
#> This should be transtlated, if 'fr' language is supported.
#> This is message number 3
#> You asked for only one item
svMisc::test_gettext_lang("fr", n = 2)
#> Test of svMisc's `gettext()` and `gettextf()`:
#> This should be transtlated, if 'fr' language is supported.
#> This is message number 3
#> You asked for several items
svMisc::test_gettext_lang("en", n = 1)
#> Test of svMisc's `gettext()` and `gettextf()`:
#> This should be transtlated, if 'en' language is supported.
#> This is message number 3
#> You asked for only one item
svMisc::test_gettext_lang("en", n = 2)
#> Test of svMisc's `gettext()` and `gettextf()`:
#> This should be transtlated, if 'en' language is supported.
#> This is message number 3
#> You asked for several items

Sys.setLanguage(old_lang) # Restore original language
rm(old_lang, gettext, gettextf, ngettext)