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
- // Reference the L10n class:
- App: import ('core', 'l10n ');
- 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
- /App/locale/eng/LC_MESSAGES/default. po (English)
- /App/locale/fre/LC_MESSAGES/default. po (French)
- /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
- Msgid "purchase" msgstr "Please purchase a ball by selecting its name ."
[Php]View plaincopy
- 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
- Msgid "purchase" msgstr "Please purchase a ball by selecting a name. "
- 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
- 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
- $ This-> Session-> write ("Config. language", "chi ");
3. use the following code to set
[Php]View plaincopy
- App: import ('core', 'l10n ');
- $ L10n = & new L10n ();
- $ L10n-> get ('Chi '); // set locale to Chinese
- // Use the _ () function to implement localization.
- _ ('Msgid ');
4) to implement localization, call the _ () function where localization is needed.
1. directly output the string without returning the value:
[Php]View plaincopy
- _ ("Msgid"); or _ ("msgid", false );
2. indirect output string with returned values:
[Php]View plaincopy
- _ ("Msgid", true );
3. add a label for the input to make the input appear in Chinese.
[Php]View plaincopy
- 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.