Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GNU gettext簡介 - 以C語言為範例

GNU gettext簡介 - 以C語言為範例

Wen_Liao

June 04, 2014
Tweet

More Decks by Wen_Liao

Other Decks in Technology

Transcript

  1. 範例 (印出訊息,支援中文和英文) #include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h>

    #include <libintl.h> #define MAX_CHAR (32) int main(int argc, char **argv) { char dest[MAX_CHAR]; char transport[MAX_CHAR]; gettext和設定語系 用到的header file
  2. 範例 (印出訊息,支援中文和英文) setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); strncpy(dest, gettext("Taipei"), MAX_CHAR);

    strncpy(transport, gettext("bus"), MAX_CHAR); printf(gettext("I will go to %s by %s.\n"), dest, transport); return 0; } 使用環境變數設定的 語言環境 指定要吃那個目錄 的哪個語言檔案c 載入語言檔案 gettext API
  3. 編譯 $ gcc -Wall -Werror -g -DPACKAGE=\" test_gettext\" -DLOCALEDIR=\" /home/user/gettext/po\"

    test_gettext.c -o test_gettext 使用者自行設定語言檔 目錄和檔案。正式使用 可放在/usr/share/locale 下面對應的語系目錄。
  4. po 目錄 tree view $ tree po/ po/ └── zh_TW

    └── LC_MESSAGES └── test_gettext.mo
  5. pot內容節錄 # SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE

    PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
  6. pot內容節錄 msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n"

    "POT-Creation-Date: 2014-06-04 12:36+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n"
  7. 翻譯po檔 msgid "Taipei" msgstr "台北" msgid "bus" msgstr "公車" msgid

    "I will go to %s by %s\n" msgstr "我搭乘%2$s到%1$s。\n" 名詞順序會隨語言不同 而改變,gettext可以在翻 譯時更改順序。
  8. 驗收成果 $ LC_ALL=en_US.utf8 ./test_gettext I will go to Taipei by

    bus $ LC_ALL=zh_TW.utf8 ./test_gettext 我搭乘公車到台北。 en_US.uft8等資料可以 由locale -a取得
  9. 同場加映, fuzzy #, fuzzy msgid "Taipei" msgstr "台北" • 產生mo後執行結果

    $ LC_ALL=zh_TW.utf8 ./test_gettext 我搭乘公車到Taipei。 , fuzzy是gettext工具語法,表 示該翻譯尚未定案,所以執行 時不會使用該翻譯。
  10. 參考資料 • Wikipedia: gettext ◦ http://en.wikipedia.org/wiki/Gettext • gettext手冊 ◦ http://www.gnu.org/software/gettext/manual/gettext.

    html • C語言中使用gettext ◦ http://wen00072-blog.logdown.com/posts/202230- study-on-gettext