yii2 multi-language settings
1. Set the default language: add: 'language'=>'zh_cn' in the mail.php configuration file
2. Multi-language switching
html code:
<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>
//语言切换 public function actionLanguage(){ $language= \Yii::$app->request->get('lang'); if(isset($language)){ \Yii::$app->session['language']=$language; } //切换完语言哪来的返回到哪里 $this->goBack(\Yii::$app->request->headers['Referer']); }
4. Modify the entry file web/index.php:
(new yiiwebApplication($config))->run();
Changed to:
$ application = new yiiwebApplication($config);
$application -> language = isset(Yii::$app->session['language']) ? Yii::$app->session['language'] : 'zh_cn';
$application -> run();
This way you can switch languages
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the multi-language settings of yii2, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.