Detailed explanation of localization and internationalization of Cakephp

黄舟
Release: 2023-03-03 16:14:02
Original
1168 people have browsed it

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
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

//Reference L10n class:

 App::import('Core', 'l10n');
Copy after login

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
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

/app/locale/eng/LC_MESSAGES/default.po (English)

/app/locale/fre/LC_MESSAGES/default.po (French)
Copy after login

/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
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

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

 view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login

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

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

 

< ;pre>

 

 

 pre> 

 

 

< 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
Copy after login
Copy after login
Copy after login
Copy after login

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
Copy after login
Copy after login
Copy after login
Copy after login

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

3. Use the following code to set up

view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

$l10n = & new L10n();

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

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

_('msgid');

4)实现本地化

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

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

view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

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

view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__("msgid",true);

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

view plaincopy
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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


 5)没有覆盖的内容

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

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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template