PHP GetText Way to implement UTF-8 internationalization multi-language-(supplemental)

Source: Internet
Author: User
Tags documentation gettext i18n locale pack

In the wangking717 of the "PHP GetText way to achieve UTF-8 internationalization of multiple languages (i18n)" (Original: http://blog.csdn.net/wangking717/article/details/4739465# Reply) After the operation of their own a little discrepancy, so note the supplementary content. Reproduced below the original text, the red word for their own supplementary content.

Recently, with the gradual standardization of I18N (internationalization), I also talk about how to achieve internationalization support in PHP. As with other programming languages, you can also use the GetText suite to write i18n programs in PHP to implement the NLS (Native Language Support) internationalization support, please refer to the official documentation (http://www.gnu.org/software/ gettext/manual/gettext.html)

here we mainly introduce the Windows platform to use PHP extension gettext implementation of the internationalization of the program.

GetText Introduction:
The GNU GetText is an important step in translating projects, providing a working framework consisting of integrated tools and documentation to help programmers, translators, and end-users implement the internationalization and localization of programs. GetText way to achieve a wide range of language support, the famous blog program WordPress internationalization is the use of the GNU GetText.

General principle:
The 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, MO file is the binary form of the 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 Application:

Step One: Build the environment (the server has been completed, the environment has been built)
1. First check to see if you have php_gettext.dll this file in your PHP extensions directory, if not, this will require you to download one or copy from another place, and then put it 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 word gettext in Phpinfo (), and the server environment is configured.

Step Two: If we want to translate the words "Hello word!" in the test.php page.

Editor test.php, the text to be translated is included with the GetText function, which means that the contained text needs to be translated.

[PHP] view plain copy    Include_once (' inc/setlan.php ');   $domain = ' Test '; Bindtextdomain ($domain, "locale/");/set Mo file path for a domain Bind_textdomain_codeset ($domain, ' UTF-8 '); The encoding of the Mo file is UTF-8 textdomain ($domain); Set the GetText () function from which domain to find Mo file?>

Here the setlan.php component is used to receive language parameters, when the call TEST.PHP?LAN=ZH_CN, then display the Chinese translation of the page, call TEST.PHP?LAN=ZH_TW, then show the traditional translation of the page, when there is no parameter, Then the default is displayed according to the language in the HTTP header information, if the language in the header information is not provided by the language pack, the text contained in the GetText function is displayed by default.
setlan.php Code:

[PHP] view plain copy <?php $lan = $_request[' lan '];    if ($lan = = ' Zh_cn ') {putenv (' lang=zh_cn '); SetLocale (Lc_all, ' zh_cn ');    Specify the language families to use, such as: en_US, ZH_CN, Zh_tw}else if ($lan = = ' Zh_tw ') {putenv (' lang=zh_tw '); SetLocale (Lc_all, ' zh_tw ');    Specify the language families to use, such as: en_US, ZH_CN, Zh_tw}else if ($lan = = ' en_US ') {putenv (' Lang=en_us '); SetLocale (Lc_all, ' en_US '); Specify the languages you want to use, such as: en_US, ZH_CN, ZH_TW}?>

step three: After the test.php is edited, we should generate the corresponding language packs (Test.po and Test.mo files) for this page .

To generate a language pack, we need to use two tools.
GetText tool Download Address: Http://nchc.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.14.4.exe (used to generate PO file, after installation, you need to "install the path/ Bin "Add to System environment variable path"
Poedit tool Download Address: http://www.poedit.net/download.php (used to edit Po file, edit the language needed to translate, and finally automatically generate MO file)

Let's say we've all installed the software, and now we're starting to work on test.php translation. 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 the page you want to translate is index.php, just change the blue part to index), and then execute, At this point you can see the newly generated file in the test.php directory Test.po

"Add 1:"

1, in the use of Poedit modify PO before remember to use other text editor to "Project-id-version:package version\n" in the "PACKAGE version" to the other version number, For example 1.0; otherwise, save failed with poedit edit;


Use the Poedit tool to open the TEST.PO, and then translate it into our corresponding language for these languages, after saving poedit will automatically produce MO files (Unicode binaries).

Put PO and Mo files into the project root directory
/locale
/language//This suggestion should be changed to en_US
/lc_messages
Test.po
Test.mo
If we put in the simplified Chinese then put:
/locale
/zh_cn
/lc_messages
Test.po
Test.mo

If we put in the traditional Chinese then put:
/locale
/zh_tw

/lc_messages
Test.po
Test.mo

"Add 2:"

2, after the update MO to restart the server, otherwise the modification does not work.

Ok. Everything went very well, and we started to visit the multilingual bar on the test. Access TEST.PHP?LAN=ZH_CN display simplified, access to test.php?lan=zh_tw display of traditional.



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.