public function actionIndex($_lang = null) { $request = Yii::$app->request; $cookies = $request->cookies; $locales = Lang::getLocaleList(false); if ($_lang !== null && isset($locales[$_lang])) { Yii::$app->response->cookies->add(new Cookie(['name' => 'language', 'value' => $_lang])); return $this->goHome(); } elseif (isset($cookies['language'])) { Yii::$app->language = ArrayHelper::getValue($locales, $cookies['language']->value); } return $this->actionView('readme'); }
<?php \yii\base\Event::on('yii\\base\\Controller', 'beforeAction', function ($event) { /** @var yii\filters\ContentNegotiator $negotiator */ $negotiator = Yii::createObject(['class' => 'yii\\filters\\ContentNegotiator', 'languages' => \common\models\Lang::getLocaleList()]); /** @var yii\base\ActionEvent $event */ $negotiator->attach($event->action); $negotiator->negotiate(); });