[R] help with gettext() for translating text

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 21 12:00:47 CET 2014


On 21/02/2014 10:40, Daniel Kelley wrote:
> I’m wondering whether anyone can help me with a translation exercise.  I have a package named “oce”, which does oceanographic processing, and I’d like to make it produce graphs with labels that work in different languages.  For example, in English I write “Depth” and in Spanish I’d like to write “Profundidad”.
>
> In my “po” directory I have R-oce.pot and es.po.  As a first step, I’ve translated just the phrases “Depth (m)” and “Depth [m]”.  Then I build and installed my package.  Within R,
>
> library(oce)
> bindtextdomain("R-oce”)

I don't think you know what that does.  And do not post HTML (see the 
posting guide): you have ended up with an invalid directional quote in 
there.

> yields
>
>   [1] "/Library/Frameworks/R.framework/Versions/3.0/Resources/library/oce/po”
>
> and then, in the OSX shell,
>
> msgunfmt /Library/Frameworks/R.framework/Versions/3.0/Resources/library/oce/po/es/LC_MESSAGES/R-oce.mo | grep -1 Depth
>
> yields
> msgid "Depth (m)"
> msgstr "Profundidad (m)"
> --
> --
> msgid "Depth [m]"
> msgstr "Profundidad [m]”
>
> so it seems that I have successfully installed the translations.  Then, I run R from the shell with
>
> LC_MESSAGES=es_ES.UTF-8 R --no-save < spanish.R
>
> where spanish.R consists of
>
> library(oce)
> cat(gettext("Depth (m)"), "\n")

Read the help:

      If ‘domain’ is ‘NULL’ or ‘""’, a domain is searched for based on
      the namespace which contains the function calling ‘gettext’ or
      ‘ngettext’.

You are not calling this from a namespace and so need to specify the 
domain rather than the default of NULL.

Example

gettext("empty model supplied")

 > gettext("empty model supplied")
[1] "empty model supplied"
 > Sys.setenv(LANGUAGE="fr")
 > gettext("empty model supplied")
[1] "empty model supplied"
 > gettext("empty model supplied", domain = "R-stats")
[1] "modèle fourni vide"

> and I get
>
> ...
> R es un software libre y viene sin GARANTIA ALGUNA.
> ...
>
> (i.e. a Spanish introduction paragraph from R) which suggests that my env-variable is OK, followed by
>
>> library(oce)
> Loading required package: mapproj
> Loading required package: maps
>> cat(gettext("Depth (m)"), "\n")
> Depth (m)
>
> which, obviously, has not translated the text.  A similar test with
>
> LANG=es_ES.UTF-8 R --no-save < spanish.R
>
> yields the same results.
>
> All of this is with R 3.0.2 on an Apple OSX platform (Mavericks); session info is below.
>
>> sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
> locale:
> [1] es_ES.UTF-8/es_ES.UTF-8/es_ES.UTF-8/C/es_ES.UTF-8/es_ES.UTF-8
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> other attached packages:
> [1] oce_0.9-14    mapproj_1.2-1 maps_2.3-2
>
>
> QUESTION: any hints on how I can get the translations to be passed through gettext()?
>
> Thanks!
>
>
> Dan E. Kelley, Professor  and Graduate Coordinator
> Oceanography Department, Dalhousie University
> PO BOX 15000
> Halifax, NS B3H 4R2
> phone:(902)494-1694 fax:(…)-3877 Dan.Kelley at Dal.CA<mailto:Dan.Kelley at Dal.CA>
> http://oceanography.dal.ca/person/Kelley_Dan.html
> http://graduatecoordinator.oceanography.dal.ca/
>
>
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list