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檔案中讀取語言資訊。

      參考[編輯]

      另見[編輯]

      外部連結[編輯]