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

Source: Internet
Author: User
Tags i18n
Example of using gettext in PHP to solve international problems (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. At present, there are many popular and most common 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 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:

The 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.

Frameworks are built with i18n support, but most of them are implemented based on PHP arrays. this method is not recommended. The most popular and common...

Related Article

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.