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