yii2 多语言设置
1.设置默认语言:在mail.php配置文件加上:'language'=>'zh_cn'
2.多语言切换
html代码 :
<a href="<?php echo Yii::$app->urlManager->createUrl(['/test/language','lang'=>'zh_cn']);?>">中文</a> <a href="<?php echo Yii::$app->urlManager->createUrl(['/test/language','lang'=>'en']);?>">English</a>
controller代码 :
//语言切换 public function actionLanguage(){ $language= \Yii::$app->request->get('lang'); if(isset($language)){ \Yii::$app->session['language']=$language; } //切换完语言哪来的返回到哪里 $this->goBack(\Yii::$app->request->headers['Referer']); }
3.多语言文件messages路径
4.修改入口文件web/index.php :
(new yii\web\Application($config))->run();
改为:
$application = new yii\web\Application($config);
$application -> language = isset(\Yii::$app->session['language']) ? \Yii::$app->session['language'] : 'zh_cn';
$application -> run();
这样就可以实现语言切换了