当前位置:编程学习 > php >>

Yii Framework 开发教程多国语言示例

总体来说,要实现信息翻译,需要执行如下几步:
 
在合适的位置调用 Yii::t() ;
以 protected/messages/LocaleID/CategoryName.php 的格式创建 PHP 翻译文件。 每个文件简单的返回一个信息翻译数组。 注意,这是假设你使用默认的 CPhpMessageSource 存储翻译信息。
配置 CApplication::sourceLanguage 和 CApplication::language。
因此第一步为Application添加合适的配置:
 
 
[php]  
<?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',  
        ),),  
);  
 
<?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 文件中定义对应的翻译:
 
 
[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'),  
    );  
}  
 
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(&
补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,