gettext库 多语言国际化2

通常人们写程序时都是将文字写死在程序里的,比如: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文件代码

Java代码   收藏代码
  1. <?php   
  2. /** 
  3.  * Dh_Locale 语言包类 
  4.  * 
  5.  * 系统语言包采用的是php-gettext模块. 
  6.  * 如果模板使用的是smarty.使用了smarty-gettext插件.插件地址http://sourceforge.net/projects/smarty-gettext/ 
  7.  *  php-gettext的安装和使用(ubuntu平台下) 
  8.  *  1 Installation of gettext package: sudo apt-get install php-gettext 
  9.  *  2 Install locales: see all locales in the file vim /usr/share/i18n/SUPPORTED 
  10.  *  3 设置文件目录结构;如: Locale/zh_CN/LC_MESSAGES 或者 Locale/en_US/LC_MESSAGES 
  11.  *  4 如果是smarty模板(使用{t}你好{/t}标记)。生成.c格式的文件;如:php -q tsmarty2c.php  $file > text.c 
  12.  *  5 生成.po格式的文件;xgettext -o Dh.po --join-existing --omit-header --no-location text.c  
  13.  *  6 生成.mo格式的文件;msgfmt Dh.po -o Dh.mo 
  14.  *  7 移动mo文件到相应的Locale/en_US/LC_MESSAGES文件夹下面 
  15.  * 
  16.  * @package  
  17.  * @version $id$ 
  18.  * @copyright 1997-2005 The PHP Group 
  19.  * @author erhuok <erhu.ok@gmail.com>  
  20.  * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt} 
  21.  */  
  22. class Dh_Locale {  
  23.     /** 
  24.      * _options 设置语言包的选项  
  25.      * 
  26.      * $this->_options['lang'] 应用程序使用什么语言包.php-gettext支持的所有语言都可以. 
  27.      * 在ubuntu下使用sudo vim /usr/share/i18n/SUPPORTED 主要是utf8编码 
  28.      * $this->_options['domain'] 生成的.mo文件的名字.一般是应用程序名 
  29.      * 
  30.      * @var array 
  31.      * @access protected 
  32.      */  
  33.     protected $_options;   
  34.     /** 
  35.      * __construct 构造函数 对象初始化时设置语言包的参数  
  36.      *  
  37.      * @access public 
  38.      * @return void 
  39.      */  
  40.     public function __construct($lang=null) {  
  41.         switch ( $lang ) {  
  42.             case 'cn':  
  43.                 $this->_options = array('lang' => 'zh_CN.utf8','domain'=>'Dh');  
  44.                 break;  
  45.             case 'en':  
  46.             case 'us':  
  47.             case 'eu':  
  48.                 $this->_options = array('lang' => 'en_US.utf8','domain'=>'Dh');  
  49.                 break;  
  50.             case 'de':  
  51.                 $this->_options = array('lang' => 'de_DE.utf8','domain'=>'Dh');  
  52.                 break;  
  53.             case 'fr':  
  54.                 $this->_options = array('lang' => 'fr_FR.utf8','domain'=>'Dh');  
  55.             default:  
  56.                 $this->_options = array('lang' => 'zh_CN.utf8','domain'=>'Dh');  
  57.                 break;  
  58.         }  
  59.         $this->setApplicationLocale();  
  60.     }  
  61.     /** 
  62.      * setOptions 设置应用程序语言包的参数 放在在数组$this->_options中  
  63.      *  
  64.      * @param mixed $options  
  65.      * @access public 
  66.      * @return void 
  67.      */  
  68.     public function setOptions($options) {  
  69.         if(!empty($options)) {  
  70.             foreach ($options as $key => $option) {  
  71.                 $this->_options[$key] = $option;  
  72.             }  
  73.         }  
  74.     }  
  75.     /** 
  76.      * setApplicationLocale  设置应用程序语言包  
  77.      *  
  78.      * @access public 
  79.      * @return void 
  80.      */  
  81.     public function setApplicationLocale() {  
  82.         putenv('LANG='.$this->_options['lang']);  
  83.         setlocale(LC_ALL,$this->_options['lang']);  
  84.         bindtextdomain($this->_options['domain'],dirname(__FILE__).'/Locale/');  
  85.         textdomain($this->_options['domain']);  
  86.         bind_textdomain_codeset($this->_options['domain'],'UTF-8');  
  87.     }  
  88. }  
  89. ?>  

2.3 测试用例

Java代码   收藏代码
  1. <?php  
  2. require_once dirname(__FILE__).'/Locale.php';  
  3. //cn or en  
  4. $Lang = 'cn';  
  5. $Locale = new Dh_Locale($Lang);  
  6. $Checkout['AddressFields'] = array  
  7. (  
  8.     'Email' => array  
  9.     (  
  10.         'Type' => 'text',  
  11.         'Label' => _('邮箱'),  
  12.         'Title' => _('请填写邮箱'),  
  13.         'Class' => 'required',  
  14.         'Filter' => 'string',  
  15.     ),  
  16.     'PostCode' => array  
  17.     (  
  18.         'Type' => 'text',  
  19.         'Label' => _('邮政编码'),  
  20.         'Title' => _('请填写邮政编码'),  
  21.         'Class' => 'required',  
  22.         'Filter' => 'int',  
  23.     ),  
  24. );  
  25. echo '<pre>';print_r($Checkout);echo '</pre>';  
  26. ?>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值