gettext

出自Local Chinese Wikipedia
跳至導覽 跳至搜尋

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文件中讀取語言信息。

      參考[編輯]

      另見[編輯]

      外部鏈接[編輯]