Example of using gettext in PHP to solve international problems (i18n)

Source: Internet
Author: User
Tags i18n

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

Gettext is used for system internationalization (I18N) and localization (L10N). You can use Native Language Support (NLS) when compiling programs )), it allows the program output to use the language set by the user instead of English. for more information about gettext, see: The following describes how to use gettext to internationalize Your PHP program.


I. Check environment requirementsFirst, check phpinfo () to make sure that the gettext extension is enabled for your PHP. If gettext is enabled, the following information should be displayed on the phpinfo page:

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

2. Create a locale folder for your projectGettext involves two files ,*. po is the source file for translation. It stores the strings to be translated in the project and the translated results ;*. the mo file is a binary file after the po file is compiled. When the translation information is actually read from the mo file, this file is also essential. Gettext has no requirements on directories. You must put international files in the specified directory. Most of the failures in using gettext are caused by the fact that the po and mo files are not in the correct location, the following is an example of a typical project directory tree:



Iii. initialize the i18n EnvironmentThis is mainly for simple settings on the program side. The following is a simple example:

Copy codeThe Code is as follows:
<? Php
// Define the target language to be translated and the encoding of the po File
$ Locale = "zh_CN.utf8 ";
Setlocale (LC_ALL, $ locale );

// Set the translation text field. 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 _ ("Hello \ n") ;;?>


4. Create a po File

There are a lot of methods at this step, of course you can create them manually, but the biggest drawback is that you don't know which strings need to be translated in the project. Here we recommend the next software-PoEdit, both Windows and Linux are applicable.

Select File> Create a message directory document and fill in the necessary information. Note that if the target language is Chinese, because the Chinese character is a double byte character, therefore, it is best to add "nplurals = 2; plural = (n! = 1); "(no quotation marks), such



Add the project folder to the path and set the keywords used for translation. PoEdit automatically searches for the strings to be translated in the project and generates the po file. After the translation is complete, select "save". PoEdit will automatically generate the mo file. In the future, the strings to be translated in each project will be updated. You only need to open PoEdit to select a category-> Update from the source. This approach is applicable not only to PHP, but also to other languages, some time ago, it was more convenient to create a po file for a django project translation. Other steps are very similar. Let's just put it apart. Pay special attention to the directory structure, which is the most prone to problems.

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.