Search code examples
yiilocale

Set locale from user function


I'm trying to just work with adjusting a web page in Yii between languages.

So far I have this simple function tha can be called from the menu bar:

    public function actionSetLanguage(){

    if(Yii::app()->language == 'en_us'){

            Yii::app()->language == 'en_uk';
    } else {

            Yii::app()->language == 'en_us';
    }

    $this->redirect(array('site/index'));


}

In my index page i'm just printing the value through Yii::app()->language.

In my config I have:

'language'=>'en_us',

My value of Yii::app()->language never changes though. It always prints out 'en_us'.

What am I doing wrong here?


Solution

  • I solved this by:

        'behaviors' => array('ApplicationConfigBehavior'),
    

    in my config:

    Followed this article:

    http://www.yiiframework.com/wiki/208/how-to-use-an-application-behavior-to-maintain-runtime-configuration/

    Changed it to sessions rather than POST.

    Controller updated to

        if(Yii::app()->language == 'en_us'){
    
            Yii::app()->session->add('lang', 'en_uk');
            Yii::app()->language == Yii::app()->session['lang'];
        } else {
    
                Yii::app()->session->remove('lang');
                Yii::app()->session->add('lang', 'en_us');
                Yii::app()->language == Yii::app()->session['lang'];
        }
    
        $this->redirect(array('site/index'));