Is it possible to use something like gettext to translate an R script. If so, how? If not, what other options I have?
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()If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With