Examples for 'base::gettext'


Translate Text Messages

Aliases: gettext ngettext bindtextdomain Sys.setLanguage

Keywords: utilities character

### ** Examples

bindtextdomain("R")  # non-null if and only if NLS is enabled
[1] "/usr/local/R/4.2/library/translations"
for(n in 0:3)
    print(sprintf(ngettext(n, "%d variable has missing values",
                              "%d variables have missing values"),
                  n))
[1] "0 variables have missing values"
[1] "1 variable has missing values"
[1] "2 variables have missing values"
[1] "3 variables have missing values"
## Not run: 
##D ## for translation, those strings should appear in R-pkg.pot as
##D msgid        "%d variable has missing values"
##D msgid_plural "%d variables have missing values"
##D msgstr[0] ""
##D msgstr[1] ""
## End(Not run)

miss <- "One only" # this line, or the next for the ngettext() below
miss <- c("one", "or", "another")
cat(ngettext(length(miss), "variable", "variables"),
    paste(sQuote(miss), collapse = ", "),
    ngettext(length(miss), "contains", "contain"), "missing values\n")
variables 'one', 'or', 'another' contain missing values
## better for translators would be to use
cat(sprintf(ngettext(length(miss),
                     "variable %s contains missing values\n",
                     "variables %s contain missing values\n"),
            paste(sQuote(miss), collapse = ", ")))
variables 'one', 'or', 'another' contain missing values
thisLang <- Sys.getenv("LANGUAGE", unset = NA) # so we can reset it
if(is.na(thisLang) || !nzchar(thisLang)) thisLang <- "en" # "factory" default
enT <- "empty model supplied"
Sys.setenv(LANGUAGE = "de") # may not always 'work'
gettext(enT, domain="R-stats")# "leeres Modell angegeben" (if translation works)
[1] "leeres Modell angegeben"
tget <- function() gettext(enT)
tget() # not translated as fn tget() is not from "stats" pkg/namespace
[1] "empty model supplied"
evalq(function() gettext(enT), asNamespace("stats"))() # *is* translated
Error in lapply(list(...), as.character): Objekt 'enT' nicht gefunden
## Sys.setLanguage()  -- typical usage --
Sys.setLanguage("en") -> oldSet # does set LANGUAGE env.var
errMsg <- function(expr) tryCatch(expr, error=conditionMessage)
(errMsg(1 + "2") -> err)
[1] "non-numeric argument to binary operator"
Sys.setLanguage("fr")
errMsg(1 + "2")
[1] "argument non numérique pour un opérateur binaire"
Sys.setLanguage("de")
errMsg(1 + "2")
[1] "nicht-numerisches Argument für binären Operator"
## Usually, you would reset the language to "previous" via
Sys.setLanguage(oldSet)

## A show off of translations -- platform (font etc) dependent:
## The translation languages available for "base" R in this version of R:
## IGNORE_RDIFF_BEGIN
if(capabilities("NLS")) withAutoprint({
  langs <- list.files(bindtextdomain("R"),
                      pattern = "^[a-z]{2}(_[A-Z]{2}|@quot)?$")
  langs
  txts <- sapply(setNames(,langs),
                 function(lang) { Sys.setLanguage(lang)
                                 gettext("incompatible dimensions", domain="R-stats") })
  cbind(txts)
  (nTrans <- length(unique(txts)))
  (not_translated <- names(txts[txts == txts[["en"]]]))
})
> langs <- list.files(bindtextdomain("R"), pattern = "^[a-z]{2}(_[A-Z]{2}|@quot)?$")
> langs
 [1] "da"      "de"      "en"      "en_GB"   "en@quot" "es"      "fa"     
 [8] "fr"      "it"      "ja"      "ko"      "lt"      "nn"      "pl"     
[15] "pt_BR"   "ru"      "tr"      "zh_CN"   "zh_TW"  
> txts <- sapply(setNames(, langs), function(lang) {
+     Sys.setLanguage(lang)
+     gettext("incompatible dimensions", domain = "R-stats")
+ })
> cbind(txts)
        txts                            
da      "incompatible dimensions"       
de      "inkompatible Dimensionen"      
en      "incompatible dimensions"       
en_GB   "incompatible dimensions"       
en@quot "incompatible dimensions"       
es      "incompatible dimensions"       
fa      "incompatible dimensions"       
fr      "dimensions incompatibles"      
it      "dimensioni incompatibili"      
ja      " 矛盾した次元です "            
ko      "incompatible dimensions"       
lt      "nesuderinami matavimo skaičiai"
nn      "incompatible dimensions"       
pl      "niezgodne wymiary"             
pt_BR   "dimensões incompatíveis"       
ru      "несовместимые размерности"     
tr      "uyumsuz boyutlar"              
zh_CN   "维度不相配"                    
zh_TW   "維度不符合"                    
> (nTrans <- length(unique(txts)))
[1] 12
> (not_translated <- names(txts[txts == txts[["en"]]]))
[1] "da"      "en"      "en_GB"   "en@quot" "es"      "fa"      "ko"     
[8] "nn"     
## IGNORE_RDIFF_END
## Here, we reset to the *original* setting before the full example started:
if(nzchar(thisLang)) { ## reset to previous and check
  Sys.setLanguage(thisLang)
  stopifnot(identical(errMsg(1 + "2"), err))
} # else staying at 'de' ..

[Package base version 4.2.3 Index]