In this example, we use the YiiFramework development tutorial (11) UI component ActiveForm to add Chinese support, which briefly describes the multi-language support. For more information, see Yii.
In this example, we use the UI component ActiveForm in Yii Framework Development Tutorial (11) to add Chinese support, which briefly describes the multi-language support. For more information, see Yii.
Information translation is implemented by calling Yii: t. This method translates information from the source language to the target language.
To achieve information translation, perform the following steps:
Call Yii: t ();
Create a php translation file in the format of protected/messages/LocaleID/CategoryName. PHP. Each file simply returns an information translation array. Note that it is assumed that you use the default CPhpMessageSource to store translation information.
Configure CApplication: sourceLanguage and CApplication: language.
Therefore, the first step is to add the appropriate configuration for the Application:
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR .'..',
'Sourcelanguage' => 'en ',
'Language' => 'zh _ cn ',
'Name' => 'i18ndemo ',
...
// Application components
'Components' => array (
'Coremessages '=> array (
'Basepath' => 'protected/messages ',
),),
);
The source language is en, the directory language is Chinese (zh_cn), and the translation information directory is protected/messages.
Then protected/messages is used to create the zh_cn Directory. to support other languages, you can create a directory and then create a yii in zh_cn. php, in which yii serves as the class name for translation (you can choose your preferred name ).
Check where the code needs to be translated, and then define the corresponding translation 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 '=> 'Select your favorite Christmas Gift ',
'IPad '=> 'iPad ',
'Remote control helicopter '=> 'Remote control helicopter ',
'60 inch 3D led TV '=> '60 inch 3D led TV ',
'Holy le Le' => 'bibb ',
'Choose your Christmas dinner '=> 'choose your Christmas dinner ',
'Egg' => 'eg ',
'Ham' => 'hams ',
'Chicken' => 'Chicken ',
'Pork' => 'pork ',
'Beer' => 'Beer ',
'Coke' => 'Cola ',
'Wine' => 'liquor ',
'Submit '=> 'submit ',
'Merry Christmas' => 'Merry Christmas ',
'On Christmas Day, You will be given' => 'You will get it On Christmas Day ',
'And you will have' => 'you can have ',
'For Christmas dinner' => 'for Christmas dinner ',
'Start Again '=> 'reselect'
);
1
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', '60 inch 3D led TV '),
'4' => Yii: t ('yii', 'Holy le Le '),
);
}
Public function getMeals ()
{
Return array (
'1' => Yii: t ('yii', 'egg '),
'2' => Yii: t ('yii', 'ham '),
'3' => Yii: t ('yii', 'chicken '),
'4' => Yii: t ('yii', 'pork '),
'5' => Yii: t ('yii', 'beer '),
'6' => Yii: t ('yii', 'Coke '),
'7' => Yii: t ('yii', 'wine '),
);
}