Optimise Multi-Language

Hello there,

I’m working on a website which must support multi-language.

On the first page, there are flags to choose the language, this is the only way to change it, and my siteController contains:


public function init() {

   $app = Yii::app();

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

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

      $this->redirect($this->createUrl('site/login'));

   }

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

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

   }

}

Then, in each controller, I’ve added:


public function init() {

   $app = Yii::app();

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

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

   }

}

I’ve tried to set in the config/main.php but it doesn’t work:


'language' => (isset(Yii::app()->session['lg']))?Yii::app()->session['lg']:'fr',

Is there a way to manage it simply ? I think creating a component/MyController.php (extending CController) and put the init function in it, and change all controllers to extend MyController. Is it the best practice ?

Thanks by advance.

Regards.

yes it is

Thanks for your confirmation.

Regards

Do it work in Yii 2.0?