问题描述
我可以在哪里设置全局语言(基于用户的 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 yiiwebController;
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 应用程序中设置全局语言值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)