Examples of using GetText to solve internationalization problems in PHP (i18n) _php

Source: Internet
Author: User
Tags gettext i18n locale phpinfo

There are many ways to internationalize, many PHP frameworks have built-in i18n support, but most of them are based on an array of PHP, this method is not recommended. At present, the most popular and most common method is gettext.

Gettext is used for system internationalization (i18n) and Localization (L10N), which enables the use of native language support when compiling programs (Native Language Support (NLS)), which allows the output of the program to be in the language of the user setting rather than in English. For more information on GetText, see: Here's how to use GetText to internationalize in your PHP program.


First , check the requirements of the Environment phpinfo () to ensure that your PHP has gettext extensions enabled. If GetText is enabled, you should see the following information in the Phpinfo page:

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

Ii. Create a new locale folder for your project GetText involves two files, *.po is the translation of the source file, which stores the project to be translated strings and translated results; *.mo file is a PO file compiled binary file, The real reading of translation information is read from the Mo file, so this file is also essential. GetText to the directory requirements, you must put the internationalization of files in the specified directory, most of the use of GetText is not successful due to the PO file and Mo file is not right side location caused by the following example to see a typical project directory tree:



Third, initialize the I18N environment This is mainly at the end of the program to make simple settings, the following gives a simple example:

Copy Code code 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");; ?>



iv. Establishment of PO file document

This step is more, of course, can be manually built, but this is the biggest drawback is that you do not know which strings in the project need to be translated, it is recommended that the next software--poedit,windows platform and Linux are applicable.

Select File-> New Message directory document, fill out some necessary information, note that if the target language is Chinese, because Chinese is a double-byte character, it is best to "fill in the plural form" nplurals=2; Plural= (n!=1); " (no quotes), as shown below



then add the folder that contains the item to the path, set the keyword for the translation, and Poedit automatically search for the strings to be translated in the project. Generate a PO file. When the translation is complete, select "Save" and Poedit will automatically generate MO files. Later on each project to be translated strings have updated, as long as open Poedit select-> from the source update, it's good this idea is not only applicable to PHP, other languages are the same, some time ago to do Django a project translation, but also only to build a PO file more convenient, The other steps are very similar. Everyone extrapolate, pay particular attention to the directory structure, this is the most prone to problems in the place.

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.