加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP中使用gettext来支持多语言的方法

发布时间:2020-12-13 05:52:53 所属栏目:PHP教程 来源:网络整理
导读:我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的),从而实现PHP的i18n. 现在,我们假设要显示一个返回主页的link: div class="codetitle" a style="CURSOR: pointer" data="202

我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的),从而实现PHP的i18n.
现在,我们假设要显示一个返回主页的link:
<div class="codetitle"><a style="CURSOR: pointer" data="2021" class="copybut" id="copybut2021" onclick="doCopy('code2021')"> 代码如下:<div class="codebody" id="code2021">
//home.php:
$str = 'home';
print <<<HTML
<a href="#">{$str}
HTML;

下面开启我们多语言的开发之旅:
创建pot文件,pot是Portable Object Template的首字母缩写,与po对应的是mo,mo是Machine Object的首字母缩写。前者意指原始的字符串文件,一般用于给翻译人员去修改的,后者则是与机器相关的,一般是供程序读取。可以手工创建pot文件,也可以通过xgettext从代码中抽取字符串来产生。这里是用xgettext来产生的:
xgettext -a home.php -o home.pot
运行该命令后,我们发现,在当前目录下,产生了一个名home.pot的文件,打开该文件,可以看到:
<div class="codetitle"><a style="CURSOR: pointer" data="93735" class="copybut" id="copybut93735" onclick="doCopy('code93735')"> 代码如下:<div class="codebody" id="code93735">
# 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 VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2009-07-23 20:56+0800n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
"Last-Translator: FULL NAME EMAIL@ADDRESSn"
"Language-Team: LANGUAGE LL@li.orgn"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=CHARSETn"
"Content-Transfer-Encoding: 8bitn"
#: 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的文件,打开该文件,可以看到:
<div class="codetitle"><a style="CURSOR: pointer" data="71709" class="copybut" id="copybut71709" onclick="doCopy('code71709')"> 代码如下:<div class="codebody" id="code71709">
# 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 VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2009-07-23 20:56+0800n"
"PO-Revision-Date: 2009-07-23 21:00+0800n"
"Last-Translator: FULL NAME EMAIL@ADDRESSn"
"Language-Team: Chinesen"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=GB2312n"
"Content-Transfer-Encoding: 8bitn"
#: test.php:2
msgid "home"
msgstr "

翻译zh_CN.po里对应的字符串为中文:
<div class="codetitle"><a style="CURSOR: pointer" data="11230" class="copybut" id="copybut11230" onclick="doCopy('code11230')"> 代码如下:<div class="codebody" id="code11230">
# 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 VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2009-07-23 20:56+0800n"
"PO-Revision-Date: 2009-07-23 21:00+0800n"
"Last-Translator: huixinchen@localhost.localdomainn"
"Language-Team: Chinesen"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=GB2312n"
"Content-Transfer-Encoding: 8bitn"
#: 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
修改程序。
<div class="codetitle"><a style="CURSOR: pointer" data="92667" class="copybut" id="copybut92667" onclick="doCopy('code92667')"> 代码如下:<div class="codebody" id="code92667">
setlocale(LC_ALL,'zh_CN');
// Specify location of translation tables
bindtextdomain("home",".");
// Choose domain
textdomain("home");
// Translation is looking for in ./locale/zh_CN/LCMESSAGES/home.mo now
$str = gettext('home'); //也可以使用
('home')
print <<<HTML
<a href="#">{$str}
HTML;

运行这个脚本,看看,是不是输出正确的中文了呢?
添加其它语言也很容易,不需要修改程序,只需要像对待中文一样,生成一个mo文件,并安装到系统中对应的目录即可。切换不同的语言仅仅是修改当前的locale就行了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读