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

查看:23
本文介绍了使用 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() 的函数的命名空间
  • stop()、message()、warning() 和 packupStartupMessage() 也被 xgettext2pot 检测到.还有一个 gettextf() 函数可用作 gettext() 的类似 sprintf 的变体.
  • 由于标准的 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天全站免登陆