Home >Backend Development >PHP Tutorial >Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial

Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:18789browse

There are many ways to achieve internationalization. Many PHP frameworks have built-in i18n support, but most of them are based on PHP arrays. This method is not recommended. Currently the most popular and versatile method is gettext.

Gettext is used for system internationalization (I18N) and localization (L10N). You can use Native Language Support (NLS) when compiling the program, which can make the output of the program use the language set by the user. Instead of English. For more information about gettext, please see: Let's talk about how to use gettext to achieve internationalization in your PHP program.


1. Check environmental requirements First check phpinfo() to make sure your PHP has the gettext extension enabled. If gettext is enabled, you should see the following information in the phpinfo page:
Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial

If not found, please modify php.ini to enable the extension

2. Create a new locale folder for your project gettext involves two files, *.po is the translation The source file stores all the strings to be translated and the translated results in the project; the *.mo file is the binary file compiled from the po file. When the translation information is actually read, it is read from the mo file, so this Documentation is also essential. Gettext has strict directory requirements. You must put the internationalized files in the specified directory. Most of the failures in using gettext are due to the po file and mo file not being placed in the right location. Here is an example of a typical project directory tree:

Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial

3. Initialize the i18n environment This is mainly a simple setting on the program side. Here is a simple example:

Copy code The code is as follows:

< ?php
//Define the target language and po file to be translated Encoding
$locale = "zh_CN.utf8";
setlocale(LC_ALL, $locale);

//Set the translation text domain. The following code will let the program go to locale/zh_CN/LC_MESSAGES/default.mo to find the translation file
bindtextdomain("default", dirname(__FILE__)."/locale") ;
textdomain("default");
?>

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
< ; ?php echo _("Hellon");; ?>



4. Create po file

There are many ways to get to this step. Of course, you can create it manually. However, the biggest disadvantage of this is that you don’t know which strings in the project need to be translated. Here is the next software recommended - PoEdit, both for Windows platform and Linux. is applicable.

Select File->Create a new message catalog document and fill in some necessary information. Note that if the target language is Chinese, since Chinese is a double-byte character, it is best to fill in "nplurals=2; plural=(n!=1);” (without quotation marks), as shown below

Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial

Then add the folder where the project is located to the "path", set the keywords used for translation, and PoEdit will automatically search for all strings to be translated in the project and generate the po file . After the translation is completed, select "Save" and PoEdit will automatically generate the mo file. In the future, every time the string to be translated in the project is updated, just open PoEdit and select the category -> Update from source. This idea is not only applicable to PHP, but is similar to other languages. I did the translation of a Django project some time ago. It's just that it's more convenient to create the po file, and the other steps are very similar. Just draw inferences from one example, and pay special attention to the directory structure, which is where problems are most likely to occur.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/802216.htmlTechArticleThere are many ways to achieve internationalization. Many php frameworks have built-in i18n support, but most of them are based on PHP arrays. Implementation, this method is not recommended. Currently the most popular and versatile...
Statement:
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