gettext                 package:base                 R Documentation

_T_r_a_n_s_l_a_t_e _T_e_x_t _M_e_s_s_a_g_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     If Native Language Support was enabled in this build of R, attempt
     to translate character vectors or set where the translations are
     to be found.

_U_s_a_g_e:

     gettext(..., domain = NULL)

     ngettext(n, msg1, msg2, domain = NULL)

     bindtextdomain(domain, dirname = NULL)

_A_r_g_u_m_e_n_t_s:

     ...: One or more character vectors.

  domain: The 'domain' for the translation.

       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,...'.

 dirname: The directory in which to find translated message catalogs
          for the domain.

_D_e_t_a_i_l_s:

     If 'domain' is 'NULL' or '""', a domain is searched for based on
     the namespace which contains the function calling 'gettext' or
     'ngettext'.  If a suitable domain can be found, each character
     string is offered for translation, and replaced by its translation
     into the current language if one is found.

     Conventionally the domain for R warning/error messages in package
     'pkg' is '"R-pkg"', and that for C-level messages is '"pkg"'.

     For 'gettext', leading and trailing whitespace is ignored when
     looking for the translation.

     'ngettext' is used where the message needs to vary by a single
     integer.  Translating such messages is subject to very specific
     rules for different languages: see the GNU Gettext Manual.  The
     string will often contain a single instance of '%d' to be used in
     'sprintf'.  If English is used, 'msg1' is returned if 'n == 1' and
     'msg2' in all other cases.

_V_a_l_u_e:

     For 'gettext', a character vector, one element per string in
     '...'.  If translation is not enabled or no domain is found or no
     translation is found in that domain, the original strings are
     returned.

     For 'ngettext', a character string.

     For 'bindtextdomain', a character string giving the current base
     directory, or 'NULL' if setting it failed.

_S_e_e _A_l_s_o:

     'stop' and 'warning' make use of 'gettext' to translate messages.

     'xgettext' for extracting translatable strings from R source
     files.

_E_x_a_m_p_l_e_s:

     bindtextdomain("R")  # non-null iff NLS is enabled

     for(n in 0:3)
         print(sprintf(ngettext(n, "%d variable has missing values",
                                   "%d variables have missing values"),
                       n))

     ## Not run: 
     ## for translation, those strings should appear in R-pkg.pot as
     msgid        "%d variable has missing values"
     msgid_plural "%d variables have missing values"
     msgstr[0] ""
     msgstr[1] ""
     ## End(Not run)

     miss <- c("one", "or", "another")
     cat(ngettext(length(miss), "variable", "variables"),
         paste(sQuote(miss), collapse=", "),
         ngettext(length(miss), "contains", "contain"), "missing values\n")

     ## 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=", ")))

