Change language with link

I have the following code in the controller:





 function init()

    {

        parent::init();

        $app = Yii::app();

        if (isset($_POST['lg']))

        {

            $app->language = $_POST['lg'];

            $app->session['lg'] = $app->language;

        }

        else if (isset($app->session['lg']))

        {

            $app->language = $app->session['lg'];

        }

    }




How can i post with lg parameter with a CHtml::link??

By the way what’s the difference between init() and index action?

if you mean how can you generate a link with the paramater lg, this is the way to go:

http://www.yiiframework.com/wiki/48/by-example-chtml/

if i understand the question correct, example 2 is what you want

regarding your init and index action question:

init is called before executing an action, see the documentation:

http://www.yiiframework.com/doc/api/1.1/CController#init-detail

the index action is simply the action that is being called when

a user requests the index of a controller (~page)

guess you may want to modify the code a bit and use the link @3one provided to add lg parameter in your chtml::link


//~~~

else if (isset($_GET['lg']))

        {

            $app->language = $_GET['lg'];

            $app->session['lg'] = $app->language;

        }

//~~~

ok.Now i have the action:


 public function actionLanguage(){

        $app = Yii::app();

        if (isset($_GET['lg']))

        {

            $app->language = $_GET['lg'];

            $app->session['lg'] = $app->language;

        }

        else if (isset($app->session['lg']))

        {

            $app->language = $app->session['lg'];

        }

        $this->redirect('index');

    }

   

the links and the echo Yii::app()->language;


 <?php echo CHtml::link(CHtml::image(Yii::app()->request->baseUrl.'/images/gb.png', 'gb',array('title'=>'English')),Yii::app()->createUrl('site/language',array('lg'=>'gb'))) ?>

      <?php echo CHtml::link(CHtml::image(Yii::app()->request->baseUrl.'/images/pt.png', 'pt',array('title'=>'Português')),Yii::app()->createUrl('site/language',array('lg'=>'pt'))) ?>

      <?php echo Yii::app()->language; ?>

    

but when i click in pt flag i see always Yii::app->language en

doesnt change

I suggest writing it like this:


public function init() {

	$app = Yii::app();

	if (isset($app->session['lg'])) {

        	$app->language = $app->session['lg'];

	}

}


public function actionLanguage() {

	$app = Yii::app();

	if (isset($_GET['lg'])) {

        	$app->session['lg'] = $_GET['lg'];

	}

	$this->redirect('index');

}

Yes i can change the Yii::app()->language but i dont see any translation.

I have the main.php on config directory like this:


return array(

    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',

    'name' => 'Style Store',

    'theme' => 'StyleStore',

    'sourceLanguage'=>'en',

    'language'=>'en',....



In my index.php


Yii::t('app','CheckOut');

my app.php under messages/pt directory:


array(

'Total Value'=>'Valor Total',

'CheckOut'=>'Finalizar Encomenda',

'Select the keyboard'  =>'Seleccione o Teclado' )

?>

Ok miss the return in app.php file,thanks guys