where to place Yii::app()->language = $_SESSION['lang '] to affect ALL Yii translations?
If placed in开发者_如何学运维 protected/views/layouts/main.php ,
Yii::app()->language = $_SESSION['lang'];
will not affect modules (because most of the contents is generated earlier). For instance: Yii-user module.
What is the best file to place the Yii::app()->language = $lang;
in the Yii flow of includes, to affect all the modules and their language settings ?
You can set up a "base" controller to extend your controllers (if you don't already have such a set-up) and put your language settings there as described here: http://www.yiiframework.com/wiki/26/setting-and-maintaining-the-language-in-application-i18n/
However, if you do have the value in a regular PHP session and want to set a global default, you should also be able to set it in your main config file with something like:
return array(
'language' => $_SESSION['lang'],
'name' => 'My Web Application',
... rest of your config settings...
I've modified protected/components/Controller.php
, adding
function init()
{
parent::init();
Yii::app()->language = $_SESSION['lang']; //or some more code;
}
It works very well.
I've also tried making MyController.php in the same directory and extending the Controller.php
, and it also works, but it slows down the whole application (most probably the loader tries hard until it loads the class, or because the operation was performed every single time the class was called ;) ).
When reading $_SESSION in the config/main.php we have to remember, that if Yii handles sessions, the second session_start() will result in a PHP E_Notice
.
Yii can handle sessions : link, and it's supposed to have them turned on by default, but when adding $_SESSION code to the config.php I needed to add a 'manual' session_start() myself.
精彩评论