Set global language value in Yii2 application(在 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 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 应用程序中设置全局语言值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Yii2 应用程序中设置全局语言值
基础教程推荐
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
