通常人們寫程序時都是將文字寫死在程序裏的,比如:echo "Hello World!"; ,假如要改成它國語言,寫國際化程序,就要逐個打開進行修改,程序較短時還行,若程序有上萬甚至更多,改起來就不是那麼容易了。近來隨着i18n的逐漸標準化,我也來講一講在PHP中如何實現國際化支持。跟其他程序語言一樣,在 PHP 也可以利用 gettext 套件寫作 i18n 程序,實現 NLS(Native Language Support) 國際化支持。
實現流程:程序設計者在程序碼中寫入所要顯示的信息,在運行程序時並不會直接顯示程序設計師所寫的信息,而會先去找一個所設置語系的信息檔。如果未找到,纔會去顯示程式碼中的信息。
一、安裝設置gettext
套件:
windows系統:
1、打開php.ini檔,查找extension=php_gettext.dll,去掉前面的“;”
2、保存,然後restart server。
若一切順利,就可以在 phpinfo() 中看到 gettext 字樣,至此已設置完畢。
二、php_gettext.dll套件裏有好幾個函式,具體請看相關的manual。在這裏我們只用記住3個函式就行了,如下:
string bindtextdomain ( string domain, string directory)
string textdomain ( string text_domain)
string gettext ( string message)
2,php-gettext的使用.以一個具體的例子說明
如果debian系列的linux用戶,注意查看本地語言支持。 vim /usr/share/i18n/SUPPORTED
中文:zh_CN.UTF-8
英文:en_US.UTF-8
德文:de_DE.UTF-8
法文:fr_FR.UTF-8
如果沒有,則相應的安裝之。方法:sudo apt-get locale-gen zh_CN.UTF-8
在php程序中,可以使用gettext()來標記需要翻譯的語言包,gettext()函數常用_()代替;
2.1 建立文件目錄
mkdir gettext
cd gettext
touch Locale.php
touch test.php
//中文mo文件的地方.
mkdir -p Locale/zh_CN/LC_MESSAGES
//英文文mo文件的地方.
mkdir -p Locale/en_US/LC_MESSAGES
至於編輯工具,由於 po 文件本身就是一個文本文件,所以任何文本編輯器都可以使用。除了專門編輯 po 文件的 poEdit
,還推薦使用 poEdit
、EditPlus
、UltraEdit
或者你喜歡的 vi
或 vim
。
漢化mo文件需要的工具叫gettext,去http://gnuwin32.sourceforge.net/packages/gettext.htm
下載一個回來安裝,然後運行:
msgunfmt.exe d:\english.mo -o d:\english.po
對english.po進行編輯、翻譯,完成後再運行:
msgfmt.exe -o d:\chinese.mo d:\english.po
然後就編譯完成了chinese.mo文件。
AccountSections.php
_('Could not retrieve the requested section please try again.');
mo文件中對應:
#: AccountSections.php:187
msgid "Could not retrieve the requested section please try again." //對應頁面調用的key
msgstr "不能取回要求的類別, 請重試" //顯示文字
2.2 Locale.php文件代碼
<?php
/**
* Dh_Locale 語言包類
*
* 系統語言包採用的是php-gettext模塊.
* 如果模板使用的是smarty.使用了smarty-gettext插件.插件地址http://sourceforge.net/projects/smarty-gettext/
* php-gettext的安裝和使用(ubuntu平臺下)
* 1 Installation of gettext package: sudo apt-get install php-gettext
* 2 Install locales: see all locales in the file vim /usr/share/i18n/SUPPORTED
* 3 設置文件目錄結構;如: Locale/zh_CN/LC_MESSAGES 或者 Locale/en_US/LC_MESSAGES
* 4 如果是smarty模板(使用{t}你好{/t}標記)。生成.c格式的文件;如:php -q tsmarty2c.php $file > text.c
* 5 生成.po格式的文件;xgettext -o Dh.po --join-existing --omit-header --no-location text.c
* 6 生成.mo格式的文件;msgfmt Dh.po -o Dh.mo
* 7 移動mo文件到相應的Locale/en_US/LC_MESSAGES文件夾下面
*
* @package
* @version $id$
* @copyright 1997-2005 The PHP Group
* @author erhuok <[email protected]>
* @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
*/
class Dh_Locale {
/**
* _options 設置語言包的選項
*
* $this->_options['lang'] 應用程序使用什麼語言包.php-gettext支持的所有語言都可以.
* 在ubuntu下使用sudo vim /usr/share/i18n/SUPPORTED 主要是utf8編碼
* $this->_options['domain'] 生成的.mo文件的名字.一般是應用程序名
*
* @var array
* @access protected
*/
protected $_options;
/**
* __construct 構造函數 對象初始化時設置語言包的參數
*
* @access public
* @return void
*/
public function __construct($lang=null) {
switch ( $lang ) {
case 'cn':
$this->_options = array('lang' => 'zh_CN.utf8','domain'=>'Dh');
break;
case 'en':
case 'us':
case 'eu':
$this->_options = array('lang' => 'en_US.utf8','domain'=>'Dh');
break;
case 'de':
$this->_options = array('lang' => 'de_DE.utf8','domain'=>'Dh');
break;
case 'fr':
$this->_options = array('lang' => 'fr_FR.utf8','domain'=>'Dh');
default:
$this->_options = array('lang' => 'zh_CN.utf8','domain'=>'Dh');
break;
}
$this->setApplicationLocale();
}
/**
* setOptions 設置應用程序語言包的參數 放在在數組$this->_options中
*
* @param mixed $options
* @access public
* @return void
*/
public function setOptions($options) {
if(!empty($options)) {
foreach ($options as $key => $option) {
$this->_options[$key] = $option;
}
}
}
/**
* setApplicationLocale 設置應用程序語言包
*
* @access public
* @return void
*/
public function setApplicationLocale() {
putenv('LANG='.$this->_options['lang']);
setlocale(LC_ALL,$this->_options['lang']);
bindtextdomain($this->_options['domain'],dirname(__FILE__).'/Locale/');
textdomain($this->_options['domain']);
bind_textdomain_codeset($this->_options['domain'],'UTF-8');
}
}
?>
2.3 測試用例
<?php
require_once dirname(__FILE__).'/Locale.php';
//cn or en
$Lang = 'cn';
$Locale = new Dh_Locale($Lang);
$Checkout['AddressFields'] = array
(
'Email' => array
(
'Type' => 'text',
'Label' => _('郵箱'),
'Title' => _('請填寫郵箱'),
'Class' => 'required',
'Filter' => 'string',
),
'PostCode' => array
(
'Type' => 'text',
'Label' => _('郵政編碼'),
'Title' => _('請填寫郵政編碼'),
'Class' => 'required',
'Filter' => 'int',
),
);
echo '<pre>';print_r($Checkout);echo '</pre>';
?>