PHP Development Framework Yii Framework Tutorial (48) Multi-Country language example

Source: Internet
Author: User
Tags documentation php file yii

This example adds Chinese support by using the Yii Framework Development tutorial (one) UI component ActiveForm example, a brief description of multinational language support. Detailed documentation can refer to the YII documentation.

Information translation is implemented by calling Yii::t (). This method translates the information from the source language into the target language.

Overall, to implement information translation, you need to perform the following steps:

Call Yii::t () at the appropriate location;

Create a PHP translation file in protected/messages/localeid/categoryname.php format. Each file simply returns an array of information translations. Note that this assumes that you are using the default Cphpmessagesource to store translation information.

Configure Capplication::sourcelanguage and Capplication::language.

So the first step is to add the appropriate configuration for application:

<?php     

//This is the main WEB application configuration. Any writable
//Cwebapplication properties can is configured here.
Return Array (
' BasePath ' =>dirname (__file__). Directory_separator. '. ', '
sourcelanguage ' => ' en ',     
' language ' => ' zh_cn ',     
' name ' => ' I18ndemo ' ,     

...
Application Components     
' components ' =>array (' coremessages ' =>array ')     
        Protected/messages ',
        ),)
;

The source language is en, the catalog language is Chinese (ZH_CN), and the directory of translation information is protected/messages.

Then protected/messages create the ZH_CN directory, and if you need to support other languages, you can create the appropriate directory, and then create a yii.php in Zh_cn, where Yii is the name of the translation (you can choose your own favorite name).

Check the code for translations, and then define the corresponding translations in the yii.php file:

Return Array (' Application name ' => ' Application name ', ' Greetings from Santa ' => ' Greetings from Santa Claus '), ' FirstName ' => ' name ', ' LastName ' => ' surname ', ' Choose your Christmas ' Gift ' => ' Choose your favorite Christmas gift ', '     
    ipad ' => ' ipad ', ' Remote control helicopter ' => ' RC Helicopter ', ' inch 3D LED TV ' => ' 60-inch 3D LED television ' ' Holy Bible ' => ' Bible, ' Choose your Christmas ' dinner ' Choose your Christmas dinner ', ' => ' Egg ' eggs ', ' => '     
    => ' ham ', ' chicken ' => ' chicken ', ' pork ' => ' pork ', ' Beer ' => ' beer ', ' Coke ' => ' Cola ', ' Wine ' => ' white wine ', ' submit ' => ', ' Merry Christmas ' => ' Merry Christmas ', ' on Christmas day,you wil     
    L be given ' => ' ' on Christmas Day you'll get ', ' and you'll have ' => ' you can have ', ' for Christmas ' dinner ' as Christmas dinner '     
' Start Again ' => ' re-selection ';
    1 then replace the original English string with Yii::t (' yii ', XXX), such as datamodel.php 1 public Function getgifts () {Return Array (' 1 ' =>yii::t (' yii ', ' IPad '), ' 2 ' =>yii::t (' yii ', ' Remote Control helicopter '),     
' 3 ' =>yii::t (' yii ', ' inch 3D LED TV '), ' 4 ' =>yii::t (' yii ', ' Holy Bible '); The Public Function getmeals () {return array (' 1 ' =>yii::t (' Yii ', ' Egg '), ' 2 ' =>yii::t I ', ' Ham '), ' 3 ' =>yii::t (' yii ', ' chicken '), ' 4 ' =>yii::t (' yii ', ' pork '), ' 5 ' =>yii::t (' yii ', ' be
Er '), ' 6 ' =>yii::t (' yii ', ' Coke '), ' 7 ' =>yii::t (' yii ', ' Wine '),); }

See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm

This example downloads: Http://www.imobilebbs.com/download/yii/I18NDemo.zip

Related Article

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.