Home > Backend Development > PHP Tutorial > yii2 multi-language settings

yii2 multi-language settings

WBOY
Release: 2016-07-29 09:15:50
Original
1172 people have browsed it

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>  
Copy after login

controller code:

//语言切换 
public function actionLanguage(){       
    $language=  \Yii::$app->request->get('lang');  
    if(isset($language)){  
        \Yii::$app->session['language']=$language;  
    }  
    //切换完语言哪来的返回到哪里
    $this->goBack(\Yii::$app->request->headers['Referer']);  
}  
Copy after login

3. Multi-language file messages path

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template