Reputation: 2769
Is it possible to use something like gettext to translate an R script. If so, how? If not, what other options I have?
Upvotes: 10
Views: 1504
Reputation: 2045
You can use base::gettext/ngettext, base::bindtextdomain and tools::xgettext2pot functions.
For example:
myFunction <- function()
{
bindtextdomain("R-myProgram","/my/translation/dir")
gettext("Hello",domain="R-myProgram")
}
Then, supposing this function is inside a file whose path is "/my/dir/R/myfile.R" use: tools::xgettext2pot("/my/dir", "/my/translation/dir/pot/R-myProgram.pot")
then use msginit, msgfmt, etc. to create a .mo file /my/translation/dir/fr/LC_MESSAGES/R-myProgram.mo
. myFunction() should now print "Bonjour" instead of "Hello" if your locale is French.
A couple of other points :
domain
argument seems to be by default the namespace of the function calling gettext()Upvotes: 12