PHP中使用gettext来支持多语言的方法
程序员文章站
2022-04-09 10:40:59
我们今天用一个简单的实例说明一下在php中的gettext的用法(gettext是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现php的i18n....
我们今天用一个简单的实例说明一下在php中的gettext的用法(gettext是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现php的i18n.
现在, 我们假设要显示一个返回主页的link:
//home.php:
$str = 'home';
print <<<html
<a href="#">{$str}</a>
html;
下面开启我们多语言的开发之旅:
创建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 <email@address>, 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 <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: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.
# <huixinchen@localhost.localdomain>, 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 <email@address>\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.
# <huixinchen@localhost.localdomain>, 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: <huixinchen@localhost.localdomain>\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 <<<html
<a href="#">{$str}</a>
html;
运行这个脚本, 看看, 是不是输出正确的中文了呢?
添加其它语言也很容易,不需要修改程序,只需要像对待中文一样,生成一个mo文件,并安装到系统中对应的目录即可。切换不同的语言仅仅是修改当前的locale就行了。
现在, 我们假设要显示一个返回主页的link:
复制代码 代码如下:
//home.php:
$str = 'home';
print <<<html
<a href="#">{$str}</a>
html;
下面开启我们多语言的开发之旅:
创建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 <email@address>, 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 <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: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.
# <huixinchen@localhost.localdomain>, 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 <email@address>\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.
# <huixinchen@localhost.localdomain>, 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: <huixinchen@localhost.localdomain>\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 <<<html
<a href="#">{$str}</a>
html;
运行这个脚本, 看看, 是不是输出正确的中文了呢?
添加其它语言也很容易,不需要修改程序,只需要像对待中文一样,生成一个mo文件,并安装到系统中对应的目录即可。切换不同的语言仅仅是修改当前的locale就行了。
上一篇: 你赢了!爸,你先送她吧!
下一篇: 借给你的五百块钱