Dear all
I want to create a site which is able to change the language of the site. So I followed the instructions shown in the reference guide of Yii, but it seems I’m doing something wrong. So what have I done so far:
-
I creates a dir messages/ with three sub directories en-UK, nl-BE, fr-BE where I have stored the php files with the translations. (I used a subdir with app in it)
-
In my views I used
<?php echo Yii::t('app/home','Title'); ?>
which refers to the translation files in the directory.
- In my view I also have three icons which add a parameter to the request, so the user can change the language of the site:
$routeBe = Url::to(['', ['language' => 'nl-BE']]);
$belgiumIcon = '<img src="' . $baseUrl . 'img/icons/Belgium.png" height="20px" width="20px">';
and then in my NavX widget I add the following to create the link with the paramter:
['label' => $belgiumIcon, 'url' => $routeBe],
Clicking on it renders
http://localhost/basic/web/site/index?1%5Blanguage%5D=nl-BE
- In my controller I added the following code in beforeAction. I first check whether the user selected another language, if not I use the language of the session.
public function beforeAction($action) {
if (parent::beforeAction($action)) {
$request = Yii::$app->request;
$language = $request->get('language');
$session = Yii::$app->session;
if ($language != null) {
$session->set('language', $language);
} else {
$language = $session->get('language');
}
Yii::$app->language = $language;
return true;
} else {
return false;
}
}
But it seems that (i) the language is not in the request (it returns null when debugging) and (ii) I cannot set the language in the session. What am I doing wrong?
Kind regards
Jens Buysse