Cakephp localization and internationalization

Source: Internet
Author: User
Tags i18n
Programmers who have read CakePHP program examples will find that in the Controller or View, most outputs are executed by a function _ ("xxxx ").

Programmers who have read CakePHP program examples will find that in the Controller or View, most outputs are executed by a function _ ("xxxx,

This function is equivalent to the getText () function in other frameworks. it dynamically obtains the corresponding language content based on the key value and locale.


1) What is i18n and l10n?
First, we need to clarify the Localization and Internationalization words, respectively, Localization and Internationalization. localization refers to converting a Web application to meet the needs of a certain language (or culture), while Internationalization indicates that a Web application can be localized. Internationalization and localization are often abbreviated to i18n and l10n. numbers 18 and 10 are the numbers between the first letter of the word and the last letter.

2) implement localization language files

Any Controller that requires localized content must first reference the L10n class of CakePHP. You can import data in AppController to all controllers. [Php]View plaincopy
  1. // Reference the L10n class:
  2. App: import ('core', 'l10n ');
  3. Class ProductController extends AppController {//...}
Next, you need to create a corresponding language file (named default. po) to manage local content. This file contains a series of strings and their IDs, which are easy to organize, manage, and translate content, and saved in the corresponding language folder. For example: [Php]View plaincopy
  1. /App/locale/eng/LC_MESSAGES/default. po (English)
  2. /App/locale/fre/LC_MESSAGES/default. po (French)
  3. /App/locale/chi/LC_MESSAGES/default. po (Chinese)

Folders containing localized content are placed in the CakePHP installation directory/app/locale. Each language corresponds to three different letter codes, which comply with the ISO 639-2 standard. (For more information, see Library of Congress Website, http://www.loc.gov/standards/iso639-2/php/code_list.php)
Once you create this file, you can edit the localized content. Note that the key value of each string must be unique and contain the corresponding value. The following is a simple example of the content of the default. po file in the English language:
[Php]View plaincopy
  1. Msgid "purchase" msgstr "Please purchase a ball by selecting its name ."
[Php]View plaincopy
  1. Msgid "search" msgstr "Click here to search our product database ."
The content of the default. po file corresponding to the Chinese language: [Php]View plaincopy
  1. Msgid "purchase" msgstr "Please purchase a ball by selecting a name. "
  2. Msgid "search" msgstr "click to search for our product list. "

[Php]View plaincopy
(Msgid is used to identify strings, which must be consistent in different language files, while msgstr is the content of the corresponding language ). the po file applies UTF-8 encoding, and each msmstr value must be within 1014 characters. If you are using a Macintosh computer, make sure to use the Unix line break (LF) when editing the file, otherwise the file will not be correctly parsed. You can easily use Poedit to edit the. po file, a free editor.

3) set the local locale of the application (this step can be omitted. cakephp will display it in the corresponding language based on the browser settings) 1. in config/core. use configure: write: in php:
[Php]View plaincopy
  1. Configure: write ('config. language', "chi ");
Chi stands for Chinese, and eng stands for English. 2. write data before the php file program starts (for example, set the corresponding language in beforeFilter based on the user's language environment ): [Php]View plaincopy
  1. $ This-> Session-> write ("Config. language", "chi ");
3. use the following code to set [Php]View plaincopy
  1. App: import ('core', 'l10n ');
  2. $ L10n = & new L10n ();
  3. $ L10n-> get ('Chi '); // set locale to Chinese
  4. // Use the _ () function to implement localization.
  5. _ ('Msgid ');


4) to implement localization, call the _ () function where localization is needed.

1. directly output the string without returning the value:

 

[Php]View plaincopy
  1. _ ("Msgid"); or _ ("msgid", false );

 

2. indirect output string with returned values:

 

[Php]View plaincopy
  1. _ ("Msgid", true );

 

3. add a label for the input to make the input appear in Chinese.

 

[Php]View plaincopy
  1. Echo $ form-> input ('name', array ('label' =>__ ('msgid', true )));

 

5) The setlocale function in php can be used to internationalize the date and currency formats of the content not covered.

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.