I’m trying to create a dropdown with two options for two languages, it’s calling an action where I set the language, but I would like to show an URL like this: http://domain.com/es/boo or http://domain.com/en/boo
And keep that structure where the user click on other action.
I have been trying to get this but I think that I’m missing something…
Thanks ORey for your response. Now I understand a bit more abour URL Manager, I see that creating a beforeAction and setting up that rule it works as you say:
Another thing, I can’t setup the language when the user clicks in the dropdown item, it should refresh the currect page, but changing the language, right? I have tried by creating an action, but it doesn’t work, any tip is more than welcome.
Controller:
protected function beforeAction($action){
if (isset($_GET['_lang'])){
Yii::app()->language = $_GET['_lang'];
}
if (empty(Yii::app()->language)){
Yii::app()->language = 'en';
}
return true;
}
public function actionLanguage(){
$this->refresh();
}
Speaking about your code, beforeAction should be placed in some base controller (like components/Controller) and all the others should extend it.
Here’s how setting language may look like:
public function actionSetLang($lang = 'en')
{
if (in_array($lang, Yii::app()->params['installedLanguages'])) {
Yii::app()->session->set('lang', $lang); // or setState, whatever you like
}
// return $this->redirect(Yii::app()->user->returnUrl);
return $this->redirect('/');
}
(sorry I may have made a couple of mistakes in this code, but you’ve got the main idea)