Hello.
In Yii1.1 i could make an action in siteController and then using: Yii::app()->controller->createUrl('actionname', array('language'=>'new language to toggle'));
It was possible to me to create hiperlinks with CHTML in index file and when clicked those hiperlinks changed the entire tranlations of the website from Portuguese to other language and all way around.
Now i'm beginning with Yii2 and controller does not have createUrl() anymore. Also the other methods dont work that way. I tried run, runAction, tried with the Url:: class and nothing.
Also, in Yii2 making <?php echo Html::a('Portuguese', Yii::$app->language = "pt"); ?>
Doesn't do anything !!! When clicked the hyperlink does not change the language of the site.
Does anyone know how to create an hiperlink or some other way in Yii 2 that toggles completely the entire website language. I need to have two versions of this website -> English and Portuguese.
I need to perform like this -> when click in the english word it will translate to English, and when click the portuguese word it will change the site to Portuguese language.
Any Ideas ??
Many thanks in advance.
NEW EDIT TO THE QUESTION I wrote this code in my siteController, and now the sites toggles languages, but only on second mouse click the toggle happens and the content is refreshed. Anybody knows why?
Here are my actions
public function beforeAction($action) {
if (Yii::$app->session->has('lang')) {
Yii::$app->language = Yii::$app->session->get('lang');
} else {
Yii::$app->language = 'us';
}
return parent::beforeAction($action);
}
public function actionLangus(){
Yii::$app->session->set('lang', 'us'); //or $_GET['lang']
return $this->render('index');
}
public function actionLangpt(){
Yii::$app->session->set('lang', 'pt'); //or $_GET['lang']
return $this->render('index');
}
I opted for render('index'), because redirect was not finding the view to display.
Many thanks for a solution...