php internationalization

巴扎黑
Release: 2016-11-24 09:58:55
Original
1941 people have browsed it

PHP’s gettext usage

The gettext program is very common in the Linux/Unix world, but ordinary people don’t have many opportunities to use it daily. It is used to add internationalization support to the application. For example, if the string resources in a program are not hard-coded in the program source file, but exist as a language pack file, then you can change the language of the program interface by adding a language pack file. gettext can do this kind of thing.

PHP5 under Windows already comes with the gettext extension, and Zend Server also comes with this extension. Remove the semicolon in front of "extension=php_gettext.dll" in the php.ini file and restart Apache. If everything is normal, there should be a gettext section in the output of phpinfo(). Here is a simple "Hello World" example to see how to use gettext to support multiple languages.

Create a PHP file test.php with the following content:
putenv("LANG=zh_CN");
setlocale(LC_ALL, 'zh_CN');

bindtextdomain('test', './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 of domain test to the locale directory under the current directory. textdomain('text') tells the PHP interpreter that the gettext function should go to the test domain to find a replacement string.

Open this file in the browser, you can see that the result is "Hello World", it is just output as it is, because the Chinese language pack has not been produced yet. To allow the browser to output Chinese without changing the source program, you need to prepare two files, PO and MO files. In order to obtain these two files, you need to use the xgettext and msgfmt programs. Windows users can go to GnuWin32 to download the GetText software package, which contains these two programs.

Then create the corresponding directory structure. Create a directory such as "localezh_CNLC_MESSAGES" in 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" means the domain is "test", which will generate the test.po file in the current directory. Open it with a text editor and make the following changes:


“Content-Type: text/plain; charset=GB2312n”
“Content-Transfer-Encoding: 8bitn”

#: test.php:11
msgid “ Hello World!"
msgstr "Hello, world! "

Set the charset to the Chinese encoding "GB2312", translate the English string in the corresponding msgid into Chinese and fill it in the msgstr below. Next use msgfmt to generate MO files. MO is a file in binary format, and gettext extracts string resources from it. Execute the following command in the command prompt to get the MO file from the PO file you just modified:
msgfmt -o test.mo test.po

Finally, place test.po and test.mo in the localezh_CNLC_MESSAGES directory, refresh the browser, You can see that the display is in Chinese.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template