Home >Backend Development >PHP Tutorial >Example of using gettext to solve internationalization problems in PHP (i18n)_PHP tutorial
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:
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:
3. Initialize the i18n environment This is mainly a simple setting on the program side. Here is a simple example:
//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");; ?>
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
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.