PHP開發架構Yii Framework教程(48) 多國語言樣本

來源:互聯網
上載者:User

本例通過Yii Framework 開發教程(11) UI 組件 ActiveForm樣本添加中文支援簡要說明一下多國語言支援。詳細文檔可可以 參考Yii文檔。

資訊翻譯是通過調用 Yii::t() 實現的。此方法會將資訊從 源語言 翻譯為 目標語言。

總體來說 ,要實現資訊翻譯,需要執行如下幾步:

在合適的位置調用 Yii::t() ;

以 protected/messages/LocaleID/CategoryName.php 的格式建立 PHP 翻譯檔案。 每個檔案簡單的返回一個資訊翻譯數組。 注意 ,這是假設你使用預設的 CPhpMessageSource 儲存翻譯資訊。

配置 CApplication::sourceLanguage 和 CApplication::language。

因此第一步為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',        ),),);

源語言為en,目錄語言為中文(zh_cn) ,翻譯資訊的目錄為protected/messages.

然後再protected/messages 建立zh_cn 目錄,如果還需要支援其它語言,可以建立相應的目錄,然後在zh_cn建立一個yii.php ,其中yii 做為翻譯時的分 類名(你可以選擇你自己喜歡的名字)。

檢查代碼中需要翻譯的地方,然後在yii.php 檔案中定義對應的翻譯:

return array (                  'Application Name' => '應用程式名稱',         'Greetings from Santa'=>'來自聖誕老人的問候',         'firstName'=>'名',         'lastName'=>'姓',         'Choose your Christmas Gift'=>'選擇你喜歡的聖誕禮物',         'iPad'=>'iPad',         'Remote control helicopter'=>'遙控直升飛機',         '60 inch 3D LED TV'=>'60寸3D LED電視',         'Holy Bible'=>'聖經',         'Choose your Christmas dinner'=>'選擇你聖誕節晚餐',         'Egg'=>'雞蛋',         'Ham'=>'火腿',         'Chicken'=>'雞',         'Pork'=>'豬肉',         'Beer'=>'啤酒',         'Coke'=>'可樂',         'Wine'=>'白酒',         'Submit'=>'提交',         'Merry Christmas'=>'聖誕快樂',         'On Christmas Day,You will be given'=>'聖誕節那天你將獲得',         'And you will have'=>'你可以有',         'for Christmas dinner'=>'作為聖誕晚餐',         'Start Again'=>'重新選擇' );     1     然後將原先使用英文字串的地方換成yii::t('yii',xxx);比如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 Bible'),             );     }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'),    );}

查看全套教程:http://www.bianceng.cn/webkf/PHP/201301/35265.htm

本例下載:http://www.imobilebbs.com/download/yii/I18NDemo.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.