Internationalization is not working

Hello;

On my page I put a list of languages(links) that my application is in so that the user who wanna change the language can click on the link and with that I send to the server a paramater lang which contains the language that he has selected.

On the server I am launching the IndexAction of the sitecontroller so that it reloads the application but before that I am executing:


 \Yii::$app->language = $lang;

thinking that it will change the language in the in the view and layout sent but it is not working.

What Am I missing? thanks for your help

Are you sure that $lang is filled?

Have you traslation of your messages?

Have you insert text in Yii::t() to apply translations?

Here is my code in sitecontroller action index




    public function actionIndex($lang)

    {


self::$supportedlanguages= \app\models\Langues::findBySql('SELECT language FROM languages')->asArray()->all();

if(SiteController::$supportedlanguage!=null){

   $lang = Yii::$app->request->get('lang');

   if(array_key_exists($lang,self::$langsupporte)){

      \Yii::$app->language = $lang;

   }

}



I even did this


public function actionIndex()

    {

       \Yii::$app->language = 'fr-FR';

}

But when I echo \Yii::$app->language I found the language was still ‘en-US’. Is there any configuration that I need to put in web.php a part from :


'i18n' =>[

'translations' =>[

'app' =>['class' => 'yii\i18n\DbMessageSource']

],

]

in application component?

Thanks for helping.

I can understand now where is the problem now. The problem is my controller action is not changing the yii::$app->language proprety even it own properties.

I also noticed that it is not passing variables to view also whenever I access a variable that I declared in that action I am getting the error undefined variable.

I don’t know what to do really I am stack

It is working now but Is there a way to set a language for the whole session unless the user choose to change it and when he changes it the one he chooses to remain for the rest of his session?

Store the language in a session variable:




Yii::$app->session['language'] = $lang;



and in the Controller beforeAction get it.

What do you mean by beforeAction? and how can I do that? Does that mean for each action I have set the language if not it will take the default(en-US)?

Where should you init the language?

In the index action in the site controller);either in when changing the language or loading the index page(leads to the index action).

But what I want is whenever a language is choosen from that point the language of the application must change for the rest of the session, if the user has not changed the one choosen previously must remain without changing until he chooses another.

For that reason you store language change in action that offers this capability and then before every action you have to set preferred language, read from session.