Home >Backend Development >PHP Tutorial >php internationalization
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.