我們今天用一個簡單的實例說明一下在PHP中的getText的用法(getText是一系列的工具和庫函數,幫助程序員和翻譯人員開發多語言軟件的), 從而實現PHP的i18n.
現在, 我們假設要顯示一個返回主頁的link:
復制代碼 代碼如下:
//home.php:
$str = 'home';
print <<<HTML
<a href="#">{$str}</a>
HTML;
下面開啟我們多語言的開發之旅:
創建pot文件,pot是Portable Object Template的首字母縮寫,與po對應的是mo,mo是Machine Object的首字母縮寫。前者意指原始的字符串文件,一般用於給翻譯人員去修改的,後者則是與機器相關的,一般是供程序讀取。可以手工創建pot文件,也可以通過xgettext從代碼中抽取字符串來產生。這裡是用xgettext來產生的:
xgettext -a home.php -o home.pot
運行該命令後,我們發現,在當前目錄下,產生了一個名home.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.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-07-23 20:56+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"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: home.php:2
msgid "home"
msgstr "
根據pot產生不同語言的po文件,這裡我們先產生一個簡體中文的po文件:
export LANG=zh_CN.gb2312
msginit -l zh_CN.gb2312 -i home.pot
運行該命令後,我們發現,在當前目錄下,產生了一個名zh_CN.po的文件,打開該文件,可以看到:
復制代碼 代碼如下:
# Chinese translations for PACKAGE package
# PACKAGE 軟件包的簡體中文翻譯.
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# <[email protected]>, 2009.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-07-23 20:56+0800\n"
"PO-Revision-Date: 2009-07-23 21:00+0800\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: Chinese\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=GB2312\n"
"Content-Transfer-Encoding: 8bit\n"
#: test.php:2
msgid "home"
msgstr "
翻譯zh_CN.po裡對應的字符串為中文:
復制代碼 代碼如下:
# Chinese translations for PACKAGE package
# PACKAGE 軟件包的簡體中文翻譯.
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# <[email protected]>, 2009.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-07-23 20:56+0800\n"
"PO-Revision-Date: 2009-07-23 21:00+0800\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: Chinese\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=GB2312\n"
"Content-Transfer-Encoding: 8bit\n"
#: test.php:2
msgid "home"
msgstr "主頁
根據po文件生成mo文件。
msgfmt zh_CN.po -o zh_CN.mo
運行該命令後,我們發現,在當前目錄下,產生了一個名zh_CN.mo的文件。它是二進制的,不能用文本編輯器打開。
安裝mo文件到特定目錄中:
cp -f zh_CN.mo .local/LC_MESSAGES/home.mo
修改程序。
復制代碼 代碼如下:
setlocale(LC_ALL, 'zh_CN');
// Specify location of translation tables
bindtextdomain("home", ".");
// Choose domain
textdomain("home");
// Translation is looking for in ./locale/zh_CN/LC_MESSAGES/home.mo now
$str = gettext('home'); //也可以使用_('home')
print <<<HTML
<a href="#">{$str}</a>
HTML;
運行這個腳本, 看看, 是不是輸出正確的中文了呢?
添加其它語言也很容易,不需要修改程序,只需要像對待中文一樣,生成一個mo文件,並安裝到系統中對應的目錄即可。切換不同的語言僅僅是修改當前的locale就行了。