1. Set the localized language
In the Protected/config folder under the main.php, do the following settings:
Return Array ( ' BasePath ' =>dirname (__file__). Directory_separator. '. ', ' name ' = ' My Web application ', ' sourcelanguage ' = ' en_us ', ' language ' = > ' ZH_CN ',
2. Define translation files
Here I use the PHP file to achieve text information translation (more ways to refer to the Yii Framework Official Guide Series 48--topic: Internationalization (i18n)), through this method to achieve translation before the protected/ Messages Create a folder named Localid (in this case, ZH_CN), and then create a custom PHP file under the folder (not named Yii, because the name can only be used internally by the YII framework), in this case we create a app.php:
Return Array ( ' my web application ' = ' my web site ', ' home ' = ' homepage ', ' about ' + ' on ', ' contact ' = > ' Contact us ', ' login ' = ' sign in ', ' Logout ' + ' exit ', ';
3. Using translations
Calling the method is simple, just by using the built-in Yii::t () method call:
Yii::t (' app ', ' My Web application ')
For example, we can translate the website title and navigation bar in protected/views/layouts/main.php:
<div id= "header" > <div id= "logo" ><?php Echo chtml::encode (yii::t (' app ', Yii::app ()->name));? ></div></div><!--Header--><div id= "MainMenu" > <?php $this->widget (' Zii.widgets.CMenu ', Array ( ' Items ' =>array (' label ' =>yii::t (' app ', ' Home '), ' url ' =>array ('/ Site/index '), Array (' label ' =>yii::t (' app ', ' about '), ' url ' =>array ('/site/page ', ' view ' = ' about '), Array (' label ' =>yii::t (' app ', ' contact '), ' url ' =>array ('/site/contact ')), Array (' label ' =>yii::t (' app ', ' Login '), ' url ' =>array ('/site/login '), ' Visible ' =>yii::app ()->user->isguest), Array (' Label ' =>yii::t ' (' app ', ' Logout '). ' (‘. Yii::app ()->user->name. ', ' url ' =>array ('/site/logout '), ' visible ' =>! Yii::app ()->user->isguest)) ;?></div><!--MainMenu--
4. View File translation
For example, if we want to implement site/index.php view file translation, we need to first create the ZH_CN folder under the Protected/views/site folder, and then copy site/index.php to the site/zh_cn/folder. Then translate the index.php folder under the Zh_cn folder:
<?php /* @var $this Sitecontroller * /$this->pagetitle=yii::t ("app", Yii::app ()->name);? >
At this point, we simply completed the translation of the Site/index page, previewed as follows:
5. Learn MorePassing parameters
Definitions in the PHP translation source:
# protected/messages/zh_cn/app.php<?php return Array ( //other stuff. ' The username {username} is not available. ' = ' user name {username} is invalid. ', //More other stuff. );
Invoke the implementation translation statement:
Echo yii::t (' app ', ' the username {username} is not available. ', Array (' {username} ' = $username));
Working with complex numbers
For example, if we deal with such translations:
Echo yii::t (' app ', ' The item has been added to your cart.| The {n} items have a been added to your cart. ', $num);
The code in the corresponding app.php is as follows:
# protected/messages/es_mx/app.php<?php return Array ( ' n==1#the item have been added to your cart.| n>1#the Items has been added to your cart. ' + ' is already in your shopping cart. | Everything has been put in your shopping cart. ', );
How to use Yii::t () for text information translation and file translation in Yii framework