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.

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 change the current directory 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 poEdit.

Examples

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

# 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:
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
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
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
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
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)