Best place to set application language

I have a multi language application and user’s desired language is reflected in urls:

mysite.com/<languageCode>/page/12

mysite.com/en/page/12

mysite.com/fr/page/12

so I have to wait for UrlManager to set $_GET[‘language’] parameter for me to know what is this request’s language.

My question is, when and where is the best time and place for me to use $_GET[‘language’] parameter and set Yii::$app->language based on it. It would be great if could be done in bootstrapping but that’s too soon and UrlManager has no yet set $_GET[‘language’] in that phase. I’m currently using onBeforeAction event in my application config:




'on beforeAction' => function ($event) {

        kalpok\i18n\LanguageSetter::setup();

},



any idea? better place for it? or that’s just fine there?

is there an event like on afterParseRequest ?

i suggest the init() function of your controller.

as you say, you have to wait the UrlManager, maybe you could also preload the UrlManager in the bootstrap-property of your config, have a look here:

http://www.yiiframework.com/doc-2.0/guide-structure-applications.html#bootstrap

My app has several modules and controllers, I prefer a single place for it. also having all controllers extend a parent just for this reason does not feel right to me.

I preloaded UrlManager as you said, but it seems preloading it does not make it run parseRequest() method which is what’s needed here.

so could you try to preload yii\web\UrlRule and all other neccessary classes?

Thanks a lot that’s all I need!