I18N - Something I Don't Understand About Yii::app()->Language.

I am trying to setup internationalization for a web app. A user can specify his default language. the app is suppose to pick this up and render in the language chose.

I am setting the default language in the user controller and then saving it to a session variable --> for testing purposes. I then set

Yii::app()->language = Yii::app()->user->languageCode

this sets the language to fr_ca.

When i render the site/index view, after login i get proper french labels and text.

            echo yii::app()->user->language;    -----> set to fr_ca

            echo yii::app()->language;          -----> set to fr_ca also


            echo '<h5>';

            echo Yii::t('app','Projects');      -----> this shows the proper french text.

            echo '</h5>';

As soon as i navigate to another view, like projects, i get a different result:


echo yii::app()->user->language;     ------> is still set to fr_ca

echo yii::app()->language;           ------> is now set to en_ca


Why would Yii::app()->language be set to another value????

I have set the default language in the main config file to ‘sourceLanguage’=>‘en_us’.

Where this code

Yii::app()->language = Yii::app()->user->languageCode

is located?

actionIndex of the site controller.

So there’s no assignment in actionProjects.

I think you should do this in some common place.

Like in beforeAction of Controller component (or some other class that your controllers extend)

Are you saying that i have to set it everytime i call an action? If so, then i must ensure that i run this code in a method that is common to every possible action?

I thought, once it was set it would remain. Odd. This is not something i read in the internationalization documentation.

How are others handling this?

So it is working now. Added this in the app base controller which extends Controller

Yii::app()->language = Yii::app()->user->languageId;

In UserIdentity, on successful authentification, i set Yii::app()->user->languageId

with language code specified by user. Then, everytime a controller is called, the

value in the session variable sets Yii::app()->language.


The only thing that is said about it is this: "One can configure target language in the application configuration, or change it dynamically before any internationalization occurs."