php多语言的使用(php: multi-language howto)
一般情况多语言的使用是用:
(Normally dealing with multi-language under php has 2 methods![]()
1. define或者array做语言资源, 用直接include包含
(Using define or array as the language resources, then use "include" to include the resources)
2. 用gettext, 设定locale
(Using gettext and locale)
感觉1的方法比较方便,但并不是正统处理多语言的手段。所以倾向于使用gettext.
(It is simple when using method 1, but is not a regular one. So i prefer using gettext.)
既然考虑到国际化的需要,那么我们便utf8编码.
(For considering international needs, we use UTF8 encoding.)
gettext的用法很简单,如下:
(Using gettext is also very simple, as the following![]()
1. 产生po (Create po file)
[yarco@China www]$ echo " > <?php > print 'Hello, World!'; > ?> > " > 1.php [yarco@China www]$ cat 1.php <?php print 'Hello, World!'; ?> [yarco@China www]$ xgettext -a 1.php -o 1.pot [yarco@China www]$ msginit -l zh_CN.UTF8 -i 1.pot -o 1.po 新的翻译库应该包含您的电子邮件地址,这样用户就可以对您的翻译提出意见, 同时软件维护者也可以在出现技术问题时联络您。 下面是您的电子邮件地址吗? yarco@china 请按回车键确认,或输入您的电子邮件地址。 yarco.w@gmail.com Retrieving http://www2.iro.umontreal.ca/~pinard/po/registry.cgi?team=index... done. A translation team for your language (zh) does not exist yet. If you want to create a new translation team for zh or zh_CN, please visit http://www.iro.umontreal.ca/contrib/po/HTML/teams.html http://www.iro.umontreal.ca/contrib/po/HTML/leaders.html http://www.iro.umontreal.ca/contrib/po/HTML/index.html 已创建 1.po。 [yarco@China www]$ cat 1.po # Chinese translations for PACKAGE package # PACKAGE 软件包的简体中文翻译. # Copyright (C) 2006 THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # Yarco <yarco.w@gmail.com>, 2006. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2006-05-20 19:06+0800\n" "PO-Revision-Date: 2006-05-20 19:06+0800\n" "Last-Translator: Yarco <yarco.w@gmail.com>\n" "Language-Team: Chinese\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: 1.php:3 msgid "Hello, World!" msgstr ""
2. 产生mo (Create mo file)
因为使用的是utf8编码, 但实际上当我们用msgfmt从po生成到mo时产生的却是gb2312代码.
(We use utf8, but the result of msgfmt from po to mo, is the gb2312 code.)
换句话说,当我把Hello, World翻译成"你好, 世界!"的时候,在web下运行时得到的却是gb2312代码.
(In other words, when i translate "Hello, World!" into Chinese "你好,世界!", i get gb2312 code under the web page.)
这和我们原来想使用utf8的想法相背. 这里丑陋的解决方法是用html实体替代.
(It breaks my mind(?) to use utf8. The method to resolv this problem is to use html entities.)
幸好我们有mb_convert_encoding, 如下:
(Fortunately, We have mb_convert_encoding, under the following![]()
[yarco@China www]$ php -r '
print mb_convert_encoding("你好,世界!", "HTML-ENTITIES", "UTF-8");
'
你好,世界![yarco@China www]$
接着我们将实体放到1.po文件中去,这样不管web运行下的页面是否utf8, 我们能够正确看到结果.
然后产生1.mo.
[yarco@China www]$ cat 1.po # Chinese translations for PACKAGE package # PACKAGE 软件包的简体中文翻译. # Copyright (C) 2006 THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # Yarco <yarco.w@gmail.com>, 2006. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2006-05-20 19:06+0800\n" "PO-Revision-Date: 2006-05-20 19:06+0800\n" "Last-Translator: Yarco <yarco.w@gmail.com>\n" "Language-Team: Chinese\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: 1.php:3 msgid "Hello, World!" msgstr "你好,世界!" [yarco@China www]$ mkdir -p zh_CN/LC_MESSAGES [yarco@China www]$ msgfmt 1.po -o zh_CN/LC_MESSAGES/1.mo
3. 修改1.php, 使用资源(Modify 1.php)
[yarco@China www]$ cat 1.php
<?php
bindtextdomain('1', '.');
textdomain('1');
setlocale(LC_MESSAGES, 'zh_CN');
print _('Hello, World!');
?>
中文化效果
zh_CN locale![]()

英文环境效果: (en_US locale![]()

唯一的缺憾就是当cli下, php 1.php时显示的是实体:
(The disvantage is under php cli, we get only the html entities replacing real words.)
[yarco@China www]$ php 1.php 你好,世界!
所以,在cli下用gettext, 可以直接写入中文.
(So, if you want to use gettext under php-cli, you can directly insert the Chinese.)
Note: 我这里的环境都是utf8
(Note: My enviroment is UTF8)
forced sex fantasy said,
一月 7, 2007 at 11:32 下午
Hello, nice site look this: