YiiFramework development tutorial multi-language example _ PHP Tutorial

Source: Internet
Author: User
YiiFramework development tutorial examples of multiple languages. In general, to implement information translation, perform the following steps: call Yii: t (); in the appropriate location to protectedmessagesLocaleIDCategoryName. to create a php translation file in the PHP format, perform the following steps to achieve information translation:

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:

[Php]

// 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 ',

),),

);

// 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 is used as the classification name during 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:

[Php]

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 '),

);

}

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 '),

);

}

Callback calls Yii: t () in the appropriate location; creates a php translation file in the format of protected/messages/LocaleID/CategoryName. PHP...

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.