Setting language in Yii2

I’m using this code to set language in yii


		if(isset($_GET['lang']))

		{

			setcookie("lang",  $_GET['lang'], time() + 31104000, "/"); // 86400 = 1 day

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

		}

		else if(isset($_COOKIE['lang']))

		{

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

		}

		else

		{

			\Yii::$app->language ="en";

		}

I’ve put it in \vendor\yiisoft\yii2\web\Controller.php

But I kinnda think it is not the best solution. What would be best solution to set language of my app.

I’m looking for something where I can execute my code before any action of any controller

@darioo, Did you got the solution for this problem you described. I’m also getting same problem. If you have any solution update it here it will helpful all others.

Derive your own controller from "\yii\web\Controller", implement its "beforeAction" and then derive every other controller from that base controller.

Hi darioo here ist very good Tutorial from samdark:

Also worth a look… https://github.com/codemix/yii2-localeurls