对于下表中没有列出来的语言mtk均不支持,如果需要请按照下面的FAQ自行添加。
第二篇:(4)多语言支持
多语言支持篇
(1)复制Yii Framework/ messages下的config文件到项目 的protected/messages目录下,支持的语言根据需要进行修改(比如需要中、繁,英):
'languages' => array('zh_cn', 'zh_tw', 'en'),
'sourceLanguage' => 'zh_cn',
(2)在protected/components/Controller.php添加方法
public function init() {
if (isset($_GET['setlang'])) {
setcookie("setlang", $_GET['setlang'], time() + 3600, '/');
$_COOKIE['setlang'] = $_GET['setlang'];
}
if (isset($_COOKIE['setlang'])) {
Yii::app()->language = $_COOKIE['setlang'];
}
}
//额外的知识点,输出调试方法,在项目全局$this->pr()可以调用调试程序: public function pr($pr, $exit = false) {
echo '<pre>';
print_r($pr);
echo '</pre>';
if ($exit) {
exit();
}
}
(3)在protected/config/main.php最前面添加代码:
if (isset($_GET['setlang']) && !empty($_GET['setlang'])) {
setcookie("setlang", $_GET['setlang'], time() + 3600, '/');
$_COOKIE['setlang'] = $_GET['setlang'];
}
数组可添加:
'language'=>'zh_cn',
'sourceLanguage'=>'en',
(4)方法一:页面下拉框根据用户设置
<select name="footer_preferedlanguage" onChange="window.location='?setlang='+this.options[this.selectedIndex].value">
<option value="" <?php if (isset($_GET['setlang']) && $_GET['setlang'] == '') echo 'selected';?> ><?php echo Yii::t('index', 'Choose your preferred language') ?></option>
<option value="zh_cn" <?php if (isset($_GET['setlang']) && $_GET['setlang'] == 'zh_cn') echo 'selected';?>>中文(简体)</option>
<option value="en" <?php if (isset($_GET['setlang']) && $_GET['setlang'] == 'en') echo 'selected';?>>English</option>
</select>
方法二:在页面菜单里切换
<div>
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'setLang',
'enableAjaxValidation' => false,
'action' => Yii::app()->request->baseUrl . '/user/setLangMenu', 'htmlOptions' => array('class' => 'regform'),
));
$userInfo = Yii::app()->session->get('userInfo');
$user = User::model()->findByPk($userInfo['userID']);
echo CHtml::hiddenField('User[prefLang]', $user->prefLang); $this->endWidget();
?>
<ul class="language">
<li class="aa <?php echo ($user->prefLang == 'zh_tw') ? 'kk' : '' ?>" onclick="changeLang('zh_tw');">繁</li>
<li class="aa <?php echo ($user->prefLang == 'zh_cn') ? 'kk' : '' ?>" onclick="changeLang('zh_cn');">简</li>
<li class="aa <?php echo ($user->prefLang == 'en') ? 'kk' : '' ?>" onclick="changeLang('en');">EN</li>
</ul>
</div>
添加JS控制:
<script type="text/javascript" charset="utf-8">
function changeLang(lang){
$("#User_prefLang").val(lang);
$("#setLang").submit();
}
$(".language li").bind({
mousedown:function(){
$(".language li").removeClass("kk");
$(this).addClass("kk");
},
mouseout:function(){
$(".language li").removeClass("kk1");
},
mouseover:function(){
$(this).addClass("kk1");
}
});
</script>
样式效果加到<head>
<style>
.language{
margin:0px;
padding:0px;
margin-top:10px;
margin-right:80px;
float:right;
}
.language li{
margin:0px;
padding:0px;
list-style:none;
float:left;
margin-right:10px;
display:block;
font-size:15px;
padding:2px;
color:#ABC8A8;
cursor:pointer;
}
.aa{
border:1px solid transparent;
font-weight: bold;
}
.kk,.kk1{
border:1px solid #7d8892;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px; }
</style>
在源文件的CSS/main.css有一些样式也可以根据效果调整 #mainmenu ul li a
{
} color:#ffffff; background-color:transparent; font-size:12px; font-weight:bold; text-decoration:none; padding:5px 8px;
(5)在protected/messages目录下新建各种语言的文件夹,如中、英,繁就建文件夹名为zh_cn、en、zh_tw 文件夹新建php翻译文件,文件内容是数组形式(如中文翻译):
return array(
'Home'=>'首页',
'Login'=>'登录',
'Logout'=>'退出',
'Contact'=>'联系我们',
'About'=>'公告',
'User'=>'用户',
'View file:'=>'视图文件:',
'Layout file:'=>'布局文件:',
'For more details on how to further develop this application, please read the'=>'更多详细应用的开发,请阅读',
'documentation'=>'说明文档',
'Feel free to ask in the'=>'如果您有任何问题,可以在',
'forum'=>'论坛',
'should you have any questions.'=>'自由询问。',
'Welcome to'=>'欢迎',
'Choose your preferred language' => '选择您的语言',
'Congratulations! You have successfully created your Yii application.'=>'恭喜!您已经成功创建你的Yii应用程序。',
'You may change the content of this page by modifying the following two files:'=>'你可以修改当前页的目录通过修改下面的两个文件:',
);
(6)在所有需要翻译页面输出的文字用方法Yii::t(‘翻译文件名’,’翻译文件里数组的KEY’) 例如:<?php echo Yii::t('index','View file:') ?> ,页面选择了中文,则在messages/zh_cn/index.php文件找数组key为‘View file:‘对应的翻译‘视图文件:‘作为页面输出。