Yii's I18n topic isn't enough for me.
My source lang is Turkish , target lang is English (for example)
My test controller's index action :
public function actionIndex()
{
Yii::app()->language='en';
$this->render("index");
}
This is my view file's content :
echo Yii::t('test', 'Deneme');
And lastly, this is my protected/messages/en/test.php file's content:
return array(
'Deneme' => 'Example',
);
Everything OK, it's returning Example . But as you can see, i'm setting language manually on my index action. How can i do it automatically ? Must i add Yii::app()->language='en'; to all actions? How you are using l18n on your projects ?
Note : I'm Yii and l18n noob, so please describe step by step .
Thank you.
You should set the target language in CWebApplication:beginRequest()
in protected/config/main.php
, add:
'onBeginRequest' => array('MyApp', 'beginRequest')
In protected/components, create a file MyApp.php
, and add this class:
class MyApp {
public static function beginRequest(CEvent $event) {
//set your language, theme, etc here
}
}
Remember to declare beginRequest()
as static
, or you will encounter errors like this:
https://github.com/yiisoft/yii/issues/794
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With