PHP GetText Way to achieve UTF-8 internationalization multi-language (i18n)

Source: Internet
Author: User
Tags gettext i18n

Transferred from: http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/08/09/2132603.html

Recently, with the gradual standardization of I18N (internationalization), I will also talk about how to achieve internationalization support in PHP. As with other programming languages, the GetText Suite writing i18n program can be used in PHP to implement NLS (Native Language support) internationalization, please refer to the official documentation (http://www.gnu.org/software/ gettext/manual/gettext.html)

Here we mainly introduce the use of PHP extension gettext in the window platform to realize the internationalization of the program.

GetText Introduction:
The GNU GetText is an important step in the translation project, providing a working framework that consists of a number of integrated tools and documentation to help programmers, translators, and end users achieve the internationalization and localization of their programs. With GetText way to achieve a wide range of language support, the famous blog program WordPress internationalization is used by the GNU GetText.

Approximate principle:
GNU GetText uses PO or mo files for internationalization and localization. Po means portable Object, which is a text structure that can be easily read and modified by people. Mo is a shorthand for machine object, and Mo file is the binary form of PO file. In general, a PO or mo file corresponds to a language, and if a program is to support multiple languages, each language needs its own PO or mo file.

Start the application:

Step One: Build the environment (the server has been completed, the environment has been set up)
1, first check your PHP extension directory if there is php_gettext.dll this file, if not, it will require you to download one or copy from other places, and then put in the PHP extension directory.
2. Open php.ini, look for "; Extension=php_gettext.dll", then remove the comment and restart Apache.

If all goes well, you can see the GetText word in phpinfo (), and the server environment is configured.

Step two: If we want to translate the test.php page of the Hello word! this sentence.

Edit test.php, the text to be translated is included with the GetText function, indicating that the contained text is needed for translation.

<?
Include_once (' inc/setlan.php ');
$domain = ' Test '; Domain name, you can take a meaningful name, but with the corresponding. Mo file has the same file name (not including the extension).
Bindtextdomain ($domain, "locale/"); Set the Mo file path for a domain
Bind_textdomain_codeset ($domain, ' UTF-8 '); Set the encoding of the Mo file to UTF-8
Textdomain ($domain); Set the GetText () function from which domain to find MO files
?>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>title</title>
<body bgcolor= "#FFFFFF" text= "#000000" link= "#FF9966" vlink= "#FF9966" alink= "#FFCC99" >
<?= gettext (' Hello World ')?>
</body>

Here the setlan.php component is used to receive the language parameters, when the call TEST.PHP?LAN=ZH_CN, then display the Chinese translated page, call TEST.PHP?LAN=ZH_TW, then display the traditional translated page, when there is no parameters, The default is based on the HTTP header information in the language to display, if the language in the header information we do not provide a language pack, the default display of the text contained in the GetText function.
setlan.php Code:

<?php
$lan = $_request [' LAN '];
if ($lan = = ' Zh_cn ') {
Putenv (' lang=zh_cn ');
SetLocale (Lc_all, ' zh_cn '); Specify the language you want to use, such as: en_US, ZH_CN, ZH_TW
}elseif ($lan = = ' Zh_tw ') {
Putenv (' lang=zh_tw ');
SetLocale (Lc_all, ' zh_tw '); Specify the language you want to use, such as: en_US, ZH_CN, ZH_TW
}elseif ($lan = = ' en_US ') {
Putenv (' Lang=en_us ');
SetLocale (Lc_all, ' en_US '); Specify the language you want to use, such as: en_US, ZH_CN, ZH_TW
}
?>


Step three: After editing the test.php, we should generate the corresponding language pack (Test.po and Test.mo files) for this page

To build a language pack, we need two tools:

1. GetText Tools: Http://nchc.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.14.4.exe

(Used to generate PO file, after installation, you need to add "installation path/bin" to the system environment variable path )
2, Poedit tools: http://www.poedit.net/download.php (used to edit PO files, edit the language needed to translate, and finally automatically generate MO files)

Assuming we've all installed the software, now we're ready to translate the test.php. Open command prompt cmd and switch to the directory where test.php is located.

Enter xgettext-d test test.php--from-code=utf-8 (when you want to translate the page to index.php, just change the blue part to index), and then execute, At this point you can see the newly generated file in the directory where the test.php is located test.po

Open the Test.po with the Poedit tool and translate it into our corresponding language for these languages, Poedit will automatically produce MO file (Unicode binary code) after saving.

Put PO files and Mo files into the project directory

/locale/language/lc_messages/test.po
/locale/language/lc_messages/test.mo


If we put in Simplified Chinese, then put in:

/locale/zh_cn/lc_messages/test.po
/locale/zh_cn/lc_messages/test.mo

If we put in traditional Chinese, then put in:

/locale/zh_tw/lc_messages/test.po
/locale/zh_tw/lc_messages/test.mo

Ok. Everything went very well and we started to visit the test under multi-lingual bar. Access to TEST.PHP?LAN=ZH_CN is shown in simplified, access test.php?lan=zh_tw show traditional. What's the problem, the message is discussed together:)

PHP enables internationalization of multiple languages (i18n) by include:

Finally, remember to restart Apache before you can see the effect.

++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++

How do i put multiple file language translations into the same. mo file???

For example, the test project file structure is as follows:

Now want to put the front desk file test.php,test2.php, such as the language of the file, such as the focus on the Cn.mo file, operation as follows:

You only need to set the language pack "domain" for files such as test.php,test2.php to "CN", and add to "domain CN" as an append to each PHP file when you generate the. mo file.

The contents of the test.php file are as follows:

<?
Putenv (' lang=zh_cn ');
SetLocale (Lc_all, ' zh_cn ');
Define the language file name to use
$domain = ' cn ';
Bindtextdomain ($domain, DirName (__file__). ' /locale '); Set the Mo file path for a domain
Bind_textdomain_codeset ($domain, ' UTF-8 '); Set the encoding of the Mo file to UTF-8
Textdomain ($domain); Set the GetText () function from which domain to find MO files
?>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>title</title>
<body bgcolor= "#FFFFFF" text= "#000000" link= "#FF9966" vlink= "#FF9966" alink= "#FFCC99" >
<?=gettext (' Hello World ')?>
</body>

The Generate. po File command is as follows:

xgettext-d cn test.php--from-code=utf-8

The contents of the test2.php file are as follows:

<?
Putenv (' lang=zh_cn ');
SetLocale (Lc_all, ' zh_cn ');
Define the language file name to use
$domain = ' cn ';
Bindtextdomain ($domain, DirName (__file__). ' /locale '); Set the Mo file path for a domain
Bind_textdomain_codeset ($domain, ' UTF-8 '); Set the encoding of the Mo file to UTF-8
Textdomain ($domain); Set the GetText () function from which domain to find MO files
?>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>title</title>
<body bgcolor= "#FFFFFF" text= "#000000" link= "#FF9966" vlink= "#FF9966" alink= "#FFCC99" >
<?=gettext (' Woshishui ')?>
</body>

The command to generate the. po file is as follows: (note that a command parameter-j is added at this point, indicating that the language pack is appended as an append.) )

xgettext-d cn test.php-j--from-code=utf-8

The resulting. po file results in the following:

nplurals=2; Plural= (n!=1);

Finally, the generated "Cn.po", "Cn.mo" files are copied to the folder "Locale\zh_cn\lc_messages\".

The final thing is to restart Apache.

Attached: WordPress template Chinese (poedit skills and. po. Mo Batch Generation Technology)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.