language definition


I have a problem with language definition in my Yii application.

The source and default language is ‘en_us’.

I want to enable the user choice the language (between french an english) so taht il will use Yii::t() to translate. In my config/main.php, i have : ‘sourceLanguage’=>‘en_us’

I have an action in SiteController :


         * Set the user language


        public function actionSetLanguage($language){




And in my Home page, when i do :

echo Yii::app()->getLanguage();

It displays ‘en_us’ even if i had send ‘fr_fr’ as parameter to actionSetLanguage method, the translation are not done.

Can anyone help?


Here is what I did in one of my project. I got help from some article on net but I don’t remember its URL.

This is my action which sets the cookie for language depending upon query string variable.

public function actionLanguage()


	$cookie = new CHttpCookie('lang', Yii::app()->request->getParam('l'));

	Yii::app()->request->cookies['lang'] = $cookie;



Next, I have created a component for application’s startup behaviour


class StartupBehavior extends CBehavior


        public function attach($owner)


                // set the event callback

                $owner->attachEventHandler('onBeginRequest', array($this, 'beginRequest'));



         * This method is attached to the 'onBeginRequest' event above.


        public function beginRequest(CEvent $event)


        	$cookie = Yii::app()->request->cookies['lang'];


        		$cookie->value = 'en';


			Yii::app()->language = $cookie->value;



and told application to run this on startup by specifying it in config array

return array(


   'name'=>'my app',

   'behaviors' => array(

        'onbeginRequest' => array(

            'class' => 'application.components.StartupBehavior',



   // remaining config...

This behavior sets current language according to the value in cookie.

Now when I use

$lang = Yii::app()->language;

to get current language. I get the correct value.

Hope this helps