Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basics of I18n For Yii Framework

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.

like image 830
Eray Avatar asked Aug 17 '11 21:08

Eray


1 Answers

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

like image 128
Neil McGuigan Avatar answered Sep 21 '22 10:09

Neil McGuigan