gettext

维基百科,自由的百科全书
跳转到导航 跳转到搜索

gettext
开发者GNU项目
当前版本
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    源代码库
    • {{URL|example.com|可选的显示文本}}
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    引擎
      Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
      操作系统跨平台
      类型软件开发翻译
      许可协议LGPL(函数库),GPL(工具),GFDL/GPL(文档)
      网站www.gnu.org/software/gettext/

      gettextGNU国际化与本地化(i18n)函数库。它常被用于编写多语言程序。

      开发[编辑]

      程序源代码需要进行修改以响应GNU gettext请求。多数编程语言均已通过字符封装的方式实现了对其的支持。为了减少输入量和代码量,此功能通常以标记别名“_”的形式使用,所以例如以下C语言代码:

      printf(gettext("My name is %s.\n"), my_name);
      

      应当写作:

      printf(_("My name is %s.\n"), my_name);
      

      gettext使用其中的字符串寻找对应的其他语言翻译,若没有可用翻译则返回原始内容。

      C语言外,GNU gettext还支持C++Objective-CPascal/Object Pascalsh脚本、bash脚本、Python、GNU CLISPEmacs Lisp、ibrep、GNU SmalltalkJava、GNU awkwxWidgets(通过wxLocale类)、YCP(YaST2语言)、TclPerlPHPPikeRuby以及R。用法均与在C语言上类似。

      xgettext程序从源代码生成.pot文件,作为源代码中需翻译内容的模板。一个典型的.pot文件条目应当是这样的:

      #: src/name.c:36
      msgid "My name is %s.\n"
      msgstr ""
      

      注释被直接放置在字符串前,用于帮助翻译者理解原文的内容:

      /// TRANSLATORS: Please leave %s as it is, because it is needed by the program.
      /// Thank you for contributing to this project.
      printf(_("My name is %s.\n"), my_name);
      

      本例中的注释是以 ///开头的,其作用是用于xgettext程序生成.pot模板文件。

      xgettext --add-comments=///
      

      在.pot文件中的注释应为以下形式:

      #. TRANSLATORS: Please leave %s as it is, because it is needed by the program.
      #. Thank you for contributing to this project. 
      #: src/name.c:36
      msgid "My name is %s.\n"
      msgstr ""
      

      翻译[编辑]

      翻译者需要处理的對象是.po文件,它是由msginit程序从.pot模板文件生成的。例如使用msginit初始化法语翻译文件时,我们运行以下命令:

      msginit --locale=fr --input=name.pot
      

      这将会使用指定的name.pot在当前目录创建一个fr.po,其中的一个条目应该是以下形式的:

      #: src/name.c:36
      msgid "My name is %s.\n"
      msgstr ""
      

      翻译者需要手工或使用类似PoeditgtranslatorOmegaTEmacs等工具的相应模式编辑该文件。翻译完成后,文件应为如下的样子:

      #: src/name.c:36
      msgid "My name is %s.\n"
      msgstr "Je m'appelle %s.\n"
      

      最后.po文件需要使用msgfmt编译为.mo文件以用作发布。

      执行[编辑]

      使用Unix类型操作系统的用户只需设置环境变量中的LC_MESSAGES(但是ubuntu linux 是用LANG)但是ubuntu linux 是用LANG[来源请求],程序将自动从相应的.mo文件中读取语言信息。

      参考[编辑]

      另见[编辑]

      外部链接[编辑]