使用gettext翻译R脚本 [英] Translation of R script using gettext

查看:70
本文介绍了使用gettext翻译R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用诸如gettext之类的东西来翻译R脚本.如果是这样,怎么办?如果没有,我还有什么其他选择?

Is it possible to use something like gettext to translate an R script. If so, how? If not, what other options I have?

推荐答案

您可以使用base :: gettext/ngettext,base :: bindtextdomain和tools :: xgettext2pot函数.
例如:

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")
}

然后,假设此函数位于路径为"/my/dir/R/myfile.R"的文件内,请使用:tools::xgettext2pot("/my/dir", "/my/translation/dir/pot/R-myProgram.pot"),然后使用msginit,msgfmt等创建.mo文件/my/translation/dir/fr/LC_MESSAGES/R-myProgram.mo.如果您的语言环境是法语,则myFunction()现在应该显示"Bonjour"而不是"Hello".

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.

其他几点:

  • xgettext2pot似乎假定您的项目是标准R包,并且仅在R/子目录中查找* .R文件.
  • domain参数在默认情况下似乎是调用gettext()的函数的名称空间
  • xgettext2pot也可以检测到
  • stop(),message(),warning()和packupStartupMessage().还有一个gettextf()函数,可以作为类似sprintf的gettext()的变体来使用.
  • 由于标准的xgettext实用工具似乎不支持R语法,并且由于必须使用tools :: xgettext2pot,因此标准方法会遗漏一些内容,例如消息上下文(pgettext()),标志表示类似printf的字符串,并可以为翻译者编写代码内注释(由xgettext -c提取),而无需手动修改.pot文件.
  • It seems like xgettext2pot assumes your project is a standard R package and looks only for *.R files inside the R/ subdirectory.
  • The domain argument seems to be by default the namespace of the function calling gettext()
  • stop(), message(), warning() and packupStartupMessage() are also detected by xgettext2pot. There is also a gettextf() function available as a sprintf-like variant of gettext().
  • Since the standard xgettext utility doesn't seem to support R syntax, and since one has to use tools::xgettext2pot, a couple of things will miss from the standard approach, such as message contexts (pgettext()), flags indicating printf-like strings and the possibility to write in-code comments for the translators (extracted by xgettext -c) without modifying the .pot file by hand.

这篇关于使用gettext翻译R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆