Using I18n

Hello,

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.

This wiki should point you in the right direction. Note that the wiki article is quite old. Today you will have a base controller named Controller.php already present in the protected/components directory.

/Tommy

if i understand exactly: you can set your sourcelanguage and your language in your main config

a.)

return array(

‘sourceLanguage’ => ‘tr’,

‘language’ => ‘en’,

)

b.)

or you can set it in your Controller.php (default generated into protected/components). This controller is extends the core controller definitions, and all of your controllers (default protected/controllers/*) will extend this.

the core has an action named init(). by default this will run before all actions in your controller. so its enough to write this in your components/controller.php

public function init() {

parent::init();

Yii::app()->language=‘en’;

}