讓PHP 多語言 【轉】

來源:互聯網
上載者:User
原文地址:http://www.laruence.com/2009/07/19/1003.html

開發多語言的Web應用是一件非常困難的事,各個國家的字元集的編碼方式、貨幣符號、日期格式、數字格式、文字表現都各不相同.

我們今天用一個簡單的執行個體說明一下在PHP中的getText的用法(getText是一系列的工具和庫函數,協助程式員和翻譯人員開發多語言軟體的), 從而實現PHP的i18n.

現在, 我們假設要顯示一個返回首頁的link:

//home.php: $str = 'home'; print <<下面開啟我們多語言的開發之旅:

建立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 , 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 \n" "Language-Team: LANGUAGE \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. # , 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 \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. # , 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: \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 <<運行這個指令碼, 看看, 是不是輸出正確的中文了呢?

添加其它語言也很容易,不需要修改程式,只需要像對待中文一樣,產生一個mo檔案,並安裝到系統中對應的目錄即可。切換不同的語言僅僅是修改當前的locale就行了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.