How to change the language

I have done all described in the cookbook/26 but dropdown language list is not work.


1.components/MyController.php


<?php

class MyController extends CController

{

    function init()

    {

        parent::init();

        $app = Yii::app();

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

        {

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

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

        }

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

        {

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

        }

    }

}

?>

2.components/LangBox.php


<?php

class LangBox extends CWidget

{

    public function run()

    {

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

        $this->render('langBox', array('currentLang' => $currentLang));

    }

}

3.components/views/langBox.php


<?php echo CHtml::form() ?>

    <div id="langdrop">

        <?php echo CHtml::dropDownList('_lang', $currentLang, array(

            'en' => 'English', 'ru' => 'Русский'), array('submit' => '')) ?>

    </div>

</form>

4.config/main.php

‘sourceLanguage’ => ‘en’,

‘language’ => ‘ru’,

  1. messages/ru/main.php

<?php

return array('Home' => 'Главная',);

I can change language when I set in the config/main.php ‘language’ => ‘en’,

, but dropdown selector is not work. Where is my mistake?

Can anybody help me, please.

I use this code to change the language of my site:




class SiteController extends Controller {

    public function beforeAction() {

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

            Yii::app()->language = $_GET['lang'];

        return true;

    }



And it works.

Are you shure, that you use POST method in the components/views/langBox.php ?

May be, you have not to write


$app = Yii::app(); $app->language = $_POST['_lang'];

, but


Yii::app()->language = $_POST['_lang'];

Yaroslav, thank you for your reply.

How on the base of your code I can set dropdown selector?

I need working solution.

Please delete my post.

It was my mistake. The method is work.