Cakephp localization and internationalization

Source: Internet
Author: User
Abstract :. programmers who have read CakePHP program examples will find that in the Controller or View, most outputs are executed using 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.

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:

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,

Content of the default. po file in English language:

view plaincopy

Msgid "purchase" msgstr "Please purchase a ball by selecting its name ."

view plaincopy

Msgid "search" msgstr "Click here to search our product database ."

The content of the default. po file corresponding to the Chinese language:

 view plaincopy

Msgid "purchase" msgstr "Please purchase a ball by selecting a name. "

Msgid "search" msgstr "click to search for our product list. "
 view plaincopy

 

 

 

 

 

 

 

 

 

 

 

 

 

(Msgid is used to identify strings, which should 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. use configure: write in config/core. 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 ):

 view plaincopy

$ This-> Session-> write ("Config. language", "chi ");

3. use the following code to set

view plaincopy

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

$ L10n = & new L10n ();

$ L10n-> get ('Chi '); // set locale to Chinese

// Use the _ () function to implement localization.

_ ('Msgid ');

4) implement localization

To implement localization, call the _ () function.

1. directly output the string without returning the value:

view plaincopy

_ ("Msgid"); or _ ("msgid", false );

2. indirect output string with returned values:

view plaincopy

_ ("Msgid", true );

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

view plaincopy

Echo $ form-> input ('name', array ('label' =>__ ('msgid', true )));


5) No covered content

You can use the setlocale function in php to internationalize the date and currency formats.

The above content is detailed about the localization and internationalization of Cakephp. For more information, see The PHP Chinese website (www.php1.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.