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

Source: Internet
Author: User
Tags i18n phpinfo
There are many ways to internationalize, many PHP frameworks have built-in i18n support, but most are implemented in PHP-based arrays, and this method is not recommended. The most popular and most common method is gettext.

Gettext is used for system internationalization (i18n) and Localization (L10N), which can be used to compile programs using native language support (Native Language supports (NLS)), which enables the output of a program to use the language of the user's settings instead of English. For more information on GetText, see: Here's how to use GetText to internationalize your PHP program.


I. Check the environmental requirements first check the phpinfo () to make sure that your PHP gettext extension is enabled. If GetText is enabled, the following information should be visible on the Phpinfo page:

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

Ii. Create a new locale folder for your project GetText involves two files, *.po is a translation source file, which stores the string to be translated in the project and the translated results; *.mo file is the Po file compiled binary file, The actual reading of the translation information is read from the Mo file, so this file is also necessary. GetText to the directory requirements are dead, you have to put the internationalization of files in the specified directory, most of the use of GetText is not successful because PO files and Mo files are not lil bit location caused, the following example to see a typical project directory tree:



Third, initialize the I18N environment This is basically a simple setup at the terminal, here is a simple example:

Copy the 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. Setting up PO file

This step is much more, of course, can be created manually, but 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 in 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); " (without quotation marks), as



Then add the folder of your project to the path, set the keyword used for the translation, and Poedit will automatically search for the string to be translated in the project and generate the Po file. After the translation is complete, select "Save" and Poedit will automatically generate MO files. Later in each project to be translated string has been updated, as long as the open Poedit Select Class---from the source update, good this idea not only applies to PHP, other languages are similar, the previous time to do Django a project translation, but also just to build PO file more convenient, The other steps are very similar. Everyone extrapolate, especially attention to the directory structure, this is the most prone to problems in the place.

http://www.bkjia.com/PHPjc/802216.html www.bkjia.com true http://www.bkjia.com/PHPjc/802216.html techarticle There are many ways to internationalize, many PHP frameworks have built-in i18n support, but most are implemented in PHP-based arrays, and this method is not recommended. Currently the most popular and most 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.