Multi-language site

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:

  1. 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)

  2. In my views I used

<?php echo Yii::t('app/home','Title'); ?>

which refers to the translation files in the directory.

  1. 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


  1. 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


To manually change the LANGUAGE parameter, translations are functioning normally?

In VIEW doing as below?

<?php echo Yii::t('app', 'Title');?>

Yes that works, manually changing the paramter changes the language. So the problem lies in programmatically changing the language paramter.

Okay, take a look, see if it helps you

Ok I’ll try the yii2-localeurls package and see where this gets me.

The package helpen tremendously :

I added following code in my view:

                $routeBe =  Url::to(['', 'language'=>'nl-BE']);

                $routeFr = Url::to(['', 'language'=>'fr-BE']);

                $routeUK = Url::to(['', 'language'=>'en-UK']);

which with the correct configuration in place directs the user to the correct site.

Thanks for the help!

Kind regards