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){
Yii::app()->setLanguage($language);
$this->redirect(Yii::app()->homeUrl);
}
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.
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;
Yii::app()->request->redirect(Yii::app()->request->urlReferrer);
}
Next, I have created a component for application’s startup behaviour
<?php
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'];
if(empty($cookie->value)){
$cookie->value = 'en';
}
Yii::app()->language = $cookie->value;
}
}
and told application to run this on startup by specifying it in config array