1、基於windowsphp
1.在php.ini中打開擴展php_gettext.dll,重啓php. 2.下載關於gettext的工具, 連接:https://git.oschina.net/tanjiajun/php_gettext/raw/master/gettext-0.14.4-bin.zip GetText 所需相關的 Library 安裝html
上述套件下載使用後, 會提示缺乏 Library: libexpat.dll libiconv2.dll 直接下載 : gettext-dll.tgz, 連接:https://git.oschina.net/tanjiajun/php_gettext/raw/master/gettext-dll.tgz 把兩個文件放到 C:\gettext\bin\目錄便可.(具體目錄因我的而異)git
3.新建一個測試的php模板文件,保存爲home.php,內容以下:windows
<?php //home.php: $str = 'home'; print <<<HTML <a href="#">{$str}</a> HTML; ?>
4.建立pot文件,pot是Portable Object Template的首字母縮寫,與po對應的是mo,mo是Machine Object的首字母縮寫。前者意指原始的字符串文件,通常用於給翻譯人員去修改的,後者則是與機器相關的,通常是供程序讀取。能夠手工建立pot文件,也能夠經過xgettext從代碼中抽取字符串來產生。這裏是用xgettext來產生的:dom
把home.php複製到工具gettext的bin目錄下,我這邊的是:D:\gettext\gettext-0.14.4-bin\bin這個目錄。 dos窗口來到這個目錄下,運行命令: 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: 2016-12-29 14:49+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=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" #: home.php:3 msgid "home" msgstr ""
接着,根據pot產生不一樣語言的po文件,這裏咱們先產生一個簡體中文的po文件: msginit -l zh_CN.gb2312 -i home.pot測試
運行該命令後,咱們發現,在當前目錄下,產生了一個名zh_CN.po的文件,打開,把文件中的編碼charset設置爲UTF-8,翻譯對應的字符串爲中文,最後以下:ui
# Chinese translations for PACKAGE package. # Copyright (C) 2016 THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # tjj <EMAIL@ADDRESS>, 2016. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-12-29 14:49+0800\n" "PO-Revision-Date: 2016-12-29 14:50+0800\n" "Last-Translator: tjj <EMAIL@ADDRESS>\n" "Language-Team: Chinese\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: home.php:3 msgid "home" msgstr "首頁"
根據po文件生成mo文件。編碼
msgfmt zh_CN.po -o home.mo
運行該命令後,咱們發現,在當前目錄下,產生了一個名home.mo的文件。它是二進制的,不能用文本編輯器打開。
複製mo文件到特定目錄中:
./locale/zh_CN/LC_MESSAGES/home.mo
最後修改,home.php:
<?php $language='zh_CN'; putenv("LANG=$language"); setlocale(LC_ALL, 'zh_CN'); // Specify location of translation tables bindtextdomain("home", "./locale"); // Choose domain textdomain("home"); // Translation is looking for in ./locale/zh_CN/LC_MESSAGES/home.mo now $str = gettext('home'); //也可使用_('home') print <<<HTML <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <a href="#">{$str}</a> HTML; ?>
運行這個腳本, 看看, 是否是輸出正確的中文了呢?
添加其它語言也很容易,不須要修改程序,只須要像對待中文同樣,生成一個mo文件,並複製到系統中對應的目錄便可。切換不一樣的語言僅僅是修改當前的locale就好了。
2、基於Linux
1.安裝php gettext擴展
(1)、從新編譯php,加上--with-gettext (2)、動態安裝
如今說下第二個動態安裝
一、下載同版本的php原包,解壓後進入ext目錄,目錄下即是模塊
二、進入gettext目錄下執行:
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config
(以上兩個命令的路徑根據本身系統狀況而定)
make make install
三、在php.ini裏添加上gettext.so extension = "gettext.so" 重啓php便可
2.安裝完畢後,和windonws步驟同樣,生成pot,.po和.mo的文件。