PHP Internationalization issues

Source: Internet
Author: User
Tags gettext locale pack php file domain root directory zend server

GetText Usage of PHP
GetText programs are common in the Linux/unix world, but there are few opportunities for ordinary people to use them on a daily basis. It is used to add internationalization support to your application. For example, if the string resources in a program are not hard-coded in the program source file, but exist in a language pack file, you can change the language of the program interface by adding a language pack file. GetText can do such a thing.
A gettext extension is already in the PHP5 under Windows, and Zend server also has the extension. In the php.ini file, remove the semicolon in front of the "Extension=php_gettext.dll" and restart Apache, and if it's all right, there should be a gettext section in the output of phpinfo (). Here's a simple example of "Hello world" to see how to use GetText to support multiple languages.
Create a PHP file test.php that reads as follows:
<?php
Putenv ("LANG=ZH_CN");
SetLocale (Lc_all, ' zh_cn ');
Bindtextdomain (' Testz, './locale ');
Textdomain (' Test ');
echo gettext (' Hello world! ');
?>
The first two lines of code set the locale to ' ZH_CN ', which is simplified Chinese. Bindtextdomain sets the directory for the domain test to the locale directory under the current directory. Textdomain (' text ') tells the PHP interpreter that the GetText function goes to the test field to find an alternative string.
Open this file in the browser, you can see the result is "Hello world", only the original output, because the Chinese language pack has not been made. To have the browser output Chinese without changing the source program, you need to prepare two files, PO, and Mo files. In order to obtain these two documents, it is necessary to use the Xgettext and msgfmt two programs. Windows users can go to GnuWin32 to download the GetText software package, which has these two programs.
The corresponding directory structure is then established. Create a directory such as "Locale\zh_cn\lc_messages" under the test.php directory. The directory name ZH_CN must be the same as the language name ZH_CN in the code. Then execute at the command prompt:
xgettext-d Test test.php
"-D test" indicates that domain is "test", which generates the Test.po file in the current directory. Open it with a text editor and make the following modifications:
...
"Content-type:text/plain; Charset=gb2312\n "
"Content-transfer-encoding:8bit\n"
#: Test.php:11
MsgId "Hello world!"
Msgstr "Hello, world!" ”
The charset will be set to the Chinese encoding "GB2312", the corresponding msgid in the English strings translated into Chinese into the following msgstr. The Mo file is then generated using MSGFMT. MO is a binary format file in which GetText extracts string resources from it. Execute the following command from the command prompt to get the mo file from the Po file you just modified:
Msgfmt-o Test.mo Test.po
Finally put Test.po and Test.mo in the Locale\zh_cn\lc_messages directory, refresh the browser, you can see the display is Chinese.

Download GnuWin32 Compile the Po Mo tool, put the environment variable, cmd access to the project root directory,
Execute separately: xgettext-d Hello hello.php generate PO File
Execute separately: msgfmt-o hello.mo hello.po generate MO Files



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.