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
App::import('Core', 'l10n');
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/fre/LC_MESSAGES/default.po (French)
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
view plaincopy
view plaincopy
msgid "search" msgstr "点击搜索我们的产品列表。"
view plaincopy
< pre>
(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
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)!