在Yii2应用程序中设置全局语言值 [英] Set global language value in Yii2 application

查看:81
本文介绍了在Yii2应用程序中设置全局语言值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以全局设置语言(基于用户的cookie)?如何使其在整个应用程序(控制器,视图等)中工作?

Where can I set language (based on user's cookie) globally? How to make it work in the whole application (controllers,views, etc.) ?

在文档中我找到了\Yii::$app->language = '';,但是在哪里可以编写我的逻辑以正确地更改语言?

In documentation I found \Yii::$app->language = ''; but, where I can write my logic to change the language in right way?

推荐答案

您应该使用

\Yii::$app->language = ''; 

位于所有控制器父级的控制器内部. 父类应位于components文件夹内,如果不可用,则使用类似

inside the controller that is parent to all your controllers. The parent class should be inside the components folder, and if it is not available than create the component with something like

use yii\web\Controller;
class MyController extends Controller
{
    public function init()
    {
        parent::init();
        #add your logic: read the cookie and then set the language
    }
}

在那之后,您必须确保所有控制器都扩展了新创建的MyController而不是原始控制器.

After that, you have to be sure that all your controllers extends your newly created MyController instead of the original one.

希望对您有帮助.

这篇关于在Yii2应用程序中设置全局语言值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆