Home  >  Article  >  Backend Development  >  Detailed explanation of localization and internationalization of Cakephp

Detailed explanation of localization and internationalization of Cakephp

黄舟
黄舟Original
2016-12-20 09:22:471128browse

Programmers who have seen CakePHP program examples will find that in Controller or View, most output is performed using a function __("xxxx").

This function is equivalent to getText in other frameworks () function dynamically obtains the corresponding language content based on the key value and locale. 8 1) What is i18N, L10N

must first understand the two words of Localization & INTERNATIONALONATION, which are called localization and internationalization. Localization means that we will transform web applications to meet the needs of a certain language (or culture), and the Internationalization Indicates the ability of web applications to be localized. Internationalization and localization are often abbreviated as i18n and l10n; the numbers 18 and 10 are the number of letters from the first letter to the last letter of the word


2) Implement localized language files


​​​​​​​Anything that needs localized content Controllers need to first reference 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 corresponding Language file (file name default.po) to manage localized content. The file contains a series of strings and their IDs, making it easy to organize, manage and translate the content, and is 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)

The folder containing localized content is placed in CakePHP Under the 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 the Library of Congress Website, http://www.loc.gov/standards/iso639-2/php/code_list.php)

Once you create the 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, the content 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 to search our product database."

Corresponds to the content of the Chinese language default.po file:

 view plaincopy

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

msgid     "search"      msgstr     "点击搜索我们的产品列表。"
 view plaincopy

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

< ;pre>bc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec pre>

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

1417fd9caae40516c8185eb949b251edbc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ec

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

.po file should be UTF-8 encoded, and each msmstr value must be limited to 1014 characters. If you are using a Macintosh computer, be sure to use Unix line feeds (LF) when editing the file, otherwise the file will not be parsed correctly. You can easily edit .po files using Poedit, a free editor.

3) Set the local locale of the application (this step can be omitted, cakephp will display in the corresponding language according to the user's browser settings)

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

 view plaincopy

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

where chi represents Chinese and English should be eng.

2. Write before starting the php file program (for example, set the corresponding language according to the user's locale in beforeFilter):

 view plaincopy

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

3. Use the following code to set up

view plaincopy

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

$l10n = & new L10n();

$l10n- >get('chi'); //Set locale to Chinese

  

//然后使用_()函数来实现本地化  

_('msgid');  

4)实现本地化

    在需要实现本地化的地方,调用_()函数

1、直接输出字符串,没有返回值:

view plaincopy

__("msgid"); 或者  _("msgid", false);  

 

2、间接输出字符串,有返回值:

view plaincopy

__("msgid",true);  

 

3、还有input要加个label来使他出现中文。

view plaincopy

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


 5)没有覆盖的内容

    日期、货币格式的国际化可以直接用php中的setlocale函数来实现。

 以上就是Cakephp本地化和国际化详解 的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn