CakePHP Localization and internationalization

Source: Internet
Author: User
Tags i18n
Programmers who have seen CakePHP's program examples will find that most of the output in the controller or view is done with a function __ ("XXXX"),

This function is quite similar to the GetText () function in other frames, which dynamically obtains the corresponding language content based on the key value and locale.


1) What is i18n,l10n
First of all to understand localization & internationalization two words, respectively, called Localization and internationalization. Localization represents the need to transform a web app to fit a language (or culture), while internationalization represents the ability of a Web application to be localized. Internationalization and localization are often abbreviated as i18n and l10n; numbers 18 and 10 are the number of letters between the first letter of the word and the last letter.

2) Implement localized language files

Any Controller that needs to localize the content needs to first refer to CakePHP's l10n class. It can be imported in AppController, so it can be used for all controllers.

View Plaincopy

Reference L10N class:

App::import (' Core ', ' l10n ');

Class Productcontroller extends AppController {//...}


Next, you need to create the appropriate language file (file name Default.po) to manage localized content. The file contains a series of string and its IDs, which are easy to organize and translate, and are saved in the corresponding language folder. Like what:

View Plaincopy

/app/locale/eng/lc_messages/default.po (中文版)

/app/locale/fre/lc_messages/default.po (French)

/app/locale/chi/lc_messages/default.po (Chinese)

Folders containing localized content are placed under the CakePHP installation directory/app/locale. Each language corresponds to a different 3-letter code that complies with the ISO 639-2 standard. (For more information, please refer to Library of Congress website,http://www.loc.gov/standards/iso639-2/php/code_list.php)
Once you have created the file, you can edit the localized content. Note that the key value for each string must be unique and contain the corresponding value. Here is an example of simplicity,

The contents of the English language Default.po file:

View Plaincopy

MsgId "Purchase" MSGSTR "please purchase a ball by selecting its name."

View Plaincopy

MsgId "Search" MSGSTR "Click here for search our product database."

Corresponding to the Chinese language default.po file content:

View Plaincopy

MsgId "Purchase" msgstr "please buy a ball by choosing a name." "

MsgId     "Search"      msgstr     "Click to find our product list. "
View Plaincopy

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

(where MsgId is used to identify strings and should be consistent in different language files, while MSGSTR is the content of the corresponding language)

The. po file applies UTF-8 encoding, each MSMSTR value must be limited to 1014 characters. If you are using Macintosh computers, make sure to use the Unix newline character (LF) when editing the file, otherwise the file will not be parsed correctly. You can easily use Poedit to edit. po files, a free editor.

3) Set the local locale of the app (this step can be omitted, cakephp will be displayed in the appropriate language based on the user's browser settings)

1. Use Configure::write in config/core.php:

View Plaincopy

Configure::write (' Config.language ', "Chi");

Of these, Chi stands for Chinese and English should be eng.

2, in the PHP file before the beginning of the program to write (for example, in Beforefilter according to the user's locale to set the appropriate language):

View Plaincopy

$this->session->write ("Config.language", "Chi");

3. Use the following code to set up

View Plaincopy

App::import (' Core ', ' l10n ');

$l 10n = & New l10n ();

$l 10n->get (' Chi '); Set Locale to Chinese

Then use the _ () function to implement localization

_ (' MsgId ');

4) Implement localization

Call the _ () function where you need to implement localization

1, direct output string, no return value:

View Plaincopy

__ ("MsgId"); Or _ ("MsgId", false);

2, the indirect output string, has the return value:

View Plaincopy

__ ("MsgId", true);

3, and input to add a label to make him appear in Chinese.

View Plaincopy

echo $form->input (' name ', Array (' label ' =>__ (' MsgId ', true)));


5) content that is not covered

The internationalization of the date and currency format can be implemented directly using the setlocale function in PHP.

The above is a detailed description of cakephp localization and internationalization, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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