ZENDFRAMEWORK2 Learning notes internationalization, multi-lingual

Source: Internet
Author: User

The ZF2 site language configuration item is the "translator" item in MODULE.CONFIG.HP:

<span style= "FONT-SIZE:18PX;" >    ' translator ' = Array (        ' locale ' = ' zh_cn ',//Chinese Simplified is ZF_CN, English is en_US        ' Translation_file_ Patterns ' = = Array (                ' type '     = ' gettext ',                ' base_dir ' and ' = __dir__. '/.. /language ',                ' pattern '  = '%s.mo ',//The language file is a manually generated skeleton application in some of the strings used in the language configuration),),    </span >

where "locale" specifies which language the project uses, and then ' pattern ' = '%s.mo ' in ' translation_file_patterns ', automatically maps to ' pattern ' = ' zh_cn.mo '. The file is saved/under the module/application/language/directory. At the same time, it can be seen that the directory also contains other national language files, for example, "En_us.mo", "Zh_tw.mo" and so on. In addition, you can see that the directory, each *.mo file should have a *.po file, open can see the *.po file contains the actual string we see.

*.po is a set of application specifications for the GNU GetText Project, and the GNU GetText Project is a software package that is created to support the internationalization of the program. *.po files are user-oriented, editable files that can be edited directly by the user, or edited using the Poedit.exe software. The *.mo file is a computer-oriented binary file that is compiled by the GetText package through *.po files.

We can edit the Zh_cn.po file by Poedit.exe software, add, modify, delete the string used in the ZF2 project. For example, you can add a string to the Zh_cn.po file, the string key is "My message!", the string value is "My message", then we can output the string directly in layout.phtml or view: "$this Translate ("My message!"); ".

In addition to the GETTEXT,ZF2 project, PHP arrays are supported as string inputs. The method is to write a PHP file, for example: my_zh_cn.php as follows,

<?php//module/test/language/my_zh_cn.phpreturn Array (    "my message id" = "Hello!");

Then, add a translator configuration entry in the/module/test/config/module.config.php:

/module/test/config/module.config.php    ' translator ' = = Array (        ' translation_files ' = = Array (            array (                ' type '     = ' phparray ',                ' filename '  = ' __dir__ '. '/.. /language/my_zh_cn.php ',),),    
After that, you can output the Chinese string directly in layout or view:
echo $this->translate (' My message id ');

ZF2 Library components, there are many forms inspection components zend\validator\xxxx or zend\i18n\validator\xxx, and so on, these form inspection components when the test does not pass, we will get some hint string from these components output to the page, These strings are obtained by validator through their own translator members. By default, the translator member of Validator is NULL, so the prompt string actually outputs the key of the string.

In fact, to let the validator prompt string display normal text, need to do 2 work, one is to set the default TRANSLAOTR for validator, and the other is to introduce the validator language configuration file into the ZF2 project (by default, Only introduced the ZF2 Skeleton Application language file "Zh_cn.mo"!!! )。

A) specify translator for Validtor: We need to add the code in the Onbootstrap function of the application module as follows,

module/application/module.php Public    function Onbootstrap (mvcevent $e)    {       //...               \zend\validator\abstractvalidator::setdefaulttranslator ($e->getapplication ()->getservicemanager () Get (' translator '));//Add Translator Component for validator component    }

II) Introduce the validator language configuration file into the ZF2 project: We need to add translator configuration items to the module.config.php of the application module,

module/application/config/module.config.php    ' translator ' = = Array (        ' translation_files ' = = Array (            Array (                ' type '     = ' phparray ',                ' filename '  = ' __dir__ '. '/.. /.. /.. /vendor/zf2/resources/languages/zh/zend_validate.php ',            ),            Array (                ' type '     = ' Phparray ',                ' filename '  = = __dir__. '/.. /.. /.. /vendor/zf2/resources/languages/zh/zend_captcha.php ',),),    

With these modifications, you can configure the language of the specified country for the ZF2 project.

If you want the website to dynamically change the language of the website based on the browser language or other information, you just need to load the different configuration files during the project startup phase.





ZENDFRAMEWORK2 Learning notes internationalization, multi-lingual

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.