ARES資通電子報

技術交流

L10N with PHP(下)

文 -Dennis

上一期我們提到了 L10N 的粗略介紹以及一些定義面的探討。本文則將以 Linux 環境為例來說明,WIN32 下面的 xgettext、msgfmt 程序檔可以從 ( http://switch.dl.sourceforge.net ... ext-0.10.40-bin.zip (http://switch.dl.sourceforge.net ... ext-0.10.40-bin.zip) ) 下载,需要 libiconv.dll、libintl.dll 的支援。具體的指令使用方法都是相同的。
  以上面 test.php 檔為例,
    xgettext -d message test.php

  運行後將產生一個 message.po 檔,內容如下:
  # 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.
  #
  #, fuzzy
  msgid ""
  msgstr ""
  "Project-Id-Version: PACKAGE VERSION\n"
  "Report-Msgid-Bugs-To: \n"
  "POT-Creation-Date: 2009-08-31 15:24+0800\n"
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  "Language-Team: LANGUAGE <LL@li.org>\n"
  "MIME-Version: 1.0\n"
  "Content-Type: text/plain; charset=UTF-8\n"
  "Content-Transfer-Encoding: 8bit\n"

  #: test.php:2
  msgid "資通電腦"
  msgstr ""

  "message.po" 21L, 607C 21,1 All
  # 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.
  #
  #, fuzzy
  msgid ""
  msgstr ""
  "Project-Id-Version: PACKAGE VERSION\n"
  "Report-Msgid-Bugs-To: \n"
  "POT-Creation-Date: 2009-08-31 15:24+0800\n"
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  "Language-Team: LANGUAGE <LL@li.org>\n"
  "MIME-Version: 1.0\n"
  "Content-Type: text/plain; charset=UTF-8\n"
  "Content-Transfer-Encoding: 8bit\n"

  #: test.php:2
  msgid "資通電腦"
  msgstr ""

裏面列出 test.php 檔裏所有調用 gettext 函式的字串,翻譯的時候只需將 msgid 值翻譯填入
msgstr 即可,如翻譯成中文。

  File message.po 內容如下:

  # 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.
  #
  #, fuzzy
  msgid ""
  msgstr ""
  "Project-Id-Version: PACKAGE VERSION\n"
  "Report-Msgid-Bugs-To: \n"
  "POT-Creation-Date: 2009-08-31 15:24+0800\n"
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  "Language-Team: LANGUAGE <LL@li.org>\n"
  "MIME-Version: 1.0\n"
  "Content-Type: text/plain; charset=UTF-8\n"
  "Content-Transfer-Encoding: 8bit\n"

  #: test.php:2
  msgid "資通電腦"
  msgstr ""

  "message.po" 21L, 607C 21,1 All
  # 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.
  #
  #, fuzzy
  msgid ""
  msgstr ""
  "Project-Id-Version: PACKAGE VERSION\n"
  "Report-Msgid-Bugs-To: \n"
  "POT-Creation-Date: 2009-08-31 15:24+0800\n"
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  "Language-Team: LANGUAGE <LL@li.org>\n"
  "MIME-Version: 1.0\n"
  "Content-Type: text/plain; charset=UTF-8\n"
  "Content-Transfer-Encoding: 8bit\n"

  #: test.php:2
  msgid "資通電腦"
  msgstr "ARES"

創建 MO 檔

.mo 多語文件是二進位,需要用特別的指令或是軟體來創建,檔的內容已編譯成二進位(打
開看到的都是亂碼),不能用一般的 text editor 軟體編輯。
  創建指令如下:
  msgfmt -o message.mo message.po

  運行後將產生一個 message.mo 二進位檔。
  最後將 message.po、message.mo 拷貝到相關語系(這裡翻譯的是一個英文語系,把 message.mo   message.po 放到 local/en_US/LC_MESSAGES/ )的目錄下即可。

結語

上面討論了利用 PHP gettext built-in functions 來實現應用程式的本地化。其優點:
1) 執行效率高,(有興趣的可以測試一下,其效率明顯高於把多語文件放在 php 的 array () 中和 database table 中)。
2) 多語的唯一性,也就是說一個多語在多處引用時,只需要翻譯一次就好。(例如 ehr系統中的 “員工代碼”,幾乎系統中到處都有用到,用此種方式只需要翻譯一次就好)。
3) 不會遺漏多語未本地化。(沒有翻譯的仍會顯示原來的語言,在前台一下就可以辨別)。

當然事情都有兩面性,同樣 gettext 也有缺點:
1) 必須要特別的編輯( 譯 )工具。
2) 學習 gettext 相關用法。

本期有關 PHP L10N 的應用就討論到這裏,希望能拋磚引玉,歡迎有興趣的朋友去深入研究或討論。
參考資料:
[1] GNU Gettext http://www.gnu.org/software/gettext/manual/gettext.html
[2] PHP Gettext http://www.php.net/manual/en/book.gettext.php
[3] PoEdit http://www.poedit.net/
[4] Zend http://www.zend.com
[5] KDE http://www.kde.org

>>回電子報首頁