Home > PHP Framework > YII > How to switch multi-language packages in yii2

How to switch multi-language packages in yii2

angryTom
Release: 2020-02-17 12:02:52
Original
2500 people have browsed it

How to switch multi-language packages in yii2

How to switch multi-language packages in yii2

1. Configuration components

'components' => [
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    //'basePath' => '/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                    ],
                ],
            ],
        ],
]
Copy after login

2. Create the messages directory

Create the messages directory in the directory of the same level as the web. This directory stores the language configuration file

Create messages/zh-CN/app.php, zh-CN It is the language identifier (\Yii::$app->session['language'] = 'zh-CN', that is, configured as zh-CN language), and the language configuration array is stored in app.php (the name of app.php is configured by The 'app' option of the component is determined)

The following is the app.php file content

<?php
return [
    //常用
    &#39;Action&#39; => &#39;操作&#39;,
    &#39;Search&#39; => &#39;搜索&#39;,
    &#39;Reset&#39; => &#39;重置&#39;,
];
Copy after login

#3. To implement language switching

There are two Method:

a) Each controller needs to be initialized (write the init function). In the init function, it is mainly to assign a value to Yii::$app->language. For example: Yii::$app->language = 'zh-CN'.

b) In web/index.php (entry file), change the code to create the application to the following code

$application = new yii\web\Application($config);
$application->language = isset(\Yii::$app->session[&#39;language&#39;]) ? \Yii::$app->session[&#39;language&#39;] : &#39;en&#39;;
$application->run();
Copy after login

4. Write the controller method to implement language switching

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

To switch languages, just call this method with the 'lang' parameter!

For more tips on using Yii and website building tutorials, please pay attention to Website Building Tutorial.

The above is the detailed content of How to switch multi-language packages in yii2. For more information, please follow other related articles on the PHP Chinese website!

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