1. Use gettext. Since Smarty has a corresponding gettext plug-in, I use this plug-in directly.
The steps are as follows:
1.1 Add it to the public include file The following code:
//$domain_info['lang']为cookie传进来的语言设定值$language_code = $domain_info['lang'];//界面语言设置成中文if($language_code == 'zh_CN'){//设置目标语言putenv("LANG=$language_code");setlocale(LC_ALL, $language_code);//$package为mo文件的名字$package = 'i18n_zh';//绑定mo文件的路径bindtextdomain($package, '/var/locale');//设置搜索的mo文件的文件名textdomain($package); Copy after login //指定mo文件的返回到gettext的编码bind_textdomain_codeset($package, 'UTF-8');}elseif($language_code == 'BIG5'){//界面语言设置成繁体$language_code = 'zh_TW';putenv("LANG=$language_code");setlocale(LC_ALL, $language_code);$package = 'i18n_tw';bindtextdomain($package, '/var/locale');textdomain($package);bind_textdomain_codeset($package, 'UTF-8');}else{//界面语言是英文//设置目标语言putenv("LANG=$language_code");setlocale(LC_ALL, $language_code);//$package为mo文件的名字$package = 'i18n_en';//绑定mo文件的路径bindtextdomain($package, '/var/locale');//设置搜索的mo文件的文件名textdomain($package);//指定mo文件的返回到gettext的编码bind_textdomain_codeset($package, 'UTF-8');}//*********添加国际化语言的处理结束***************/ Copy after login |
1.2 Add the t tag required by the smarty plug-in to the corresponding template
1.3 Use the tool provided by the smarty plug-in to generate the c file
This c file It extracts the tagged strings in all templates
php -q ./tsmarty2c.php *.html $package.c Copy after login |
Note: The name of the default c file is the same as the one defined in 1.1
It should be noted that tsmarty2c. The open tag and close tag of smarty defined in php must be consistent with the settings in the smarty configuration file
1.4 Call the linux system to use xgettext to generate a file with the suffix po from the c file in 1.3
xgettext -d $package $package.c Copy after login |
Note: If the file is not Asc encoded, you must specify it in the above command
--file-code=File encoding
1.5 Edit $package.po generated in 1.4 and add The corresponding translation string
is available in several languages. Just edit and generate several po files separately
1.6 Call msgfmt on the Linux system to generate the po files in 1.3 and 1.5 with the suffix mo The binary file
msgfmt -o $package.mo $package.po Copy after login |
1.7 creates the locale directory, which must be the same as bindtextdomain($package, '/var/locale'); in 1.1.
Use the settings in 1.1 For example, first create the locale directory in the /var/ (existing) directory. The structure of the locale is
|-- en_US<br>| `-- LC_MESSAGES<br>|<br>|-- $package.mo Copy after login |<br>|-- zh_CN<br>| `-- LC_MESSAGES<br>|<br>|-- $package.mo Copy after login | Copy after login `-- zh_TW<br>`-- LC_MESSAGES<br>|-- $package.mo Copy after login |
. The first-level directory is set according to the definition of $language_code in 1.1. It must be like this, otherwise it cannot be found. The second-level directory LC_MESSAGES is fixed, and it stores the mo files generated in 1.6
2. Since the website uses Smaty, we can extract all the text in the template Come out and put them into the language file together
Load it through smarty's config_load. The premise is to put the language file in the directory set by $smarty->config_dir
Just include the following code in php,
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: SimSun">$smarty->config_load( 'chs.lang' );</p><p style="FONT-SIZE: 10pt; MARGIN: 0in"><span lang="EN-US" style="FONT-FAMILY: Verdana">//$lang</span><span lang="ZH-CN" style="FONT-FAMILY: SimSun">为通过</span><span lang="EN-US" style="FONT-FAMILY: Verdana">cookie</span><span lang="ZH-CN" style="FONT-FAMILY: SimSun">或</span><span lang="EN-US" style="FONT-FAMILY: Verdana">session</span><span lang="ZH-CN" style="FONT-FAMILY: SimSun">获得的页面语言值</span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: SimSun">switch ($lang) { </p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: SimSun">case 'zh-cn' :<span> </span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">$smarty->config_load( 'chs.lang' );</p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">break;<span> </span><span> </span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: SimSun">case 'zh-tw' :<span> </span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">header( 'cht.lang' ); </p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">break;<span> </span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: SimSun">default: </p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">header( 'cht.lang' ); </p><p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: SimSun">break;<span> </span></p><p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: SimSun">}</p> Copy after login |