public function beforeAction($event)
    {
        if (!Yii::$app->errorHandler->exception && count($this->languages) > 1) {

            // Set language by GET request, session or cookie
            if ($language = Yii::$app->getRequest()->get('language')) {

                Yii::$app->language = $this->getLanguageCode($language);
                Yii::$app->session->set('language', Yii::$app->language);
                Yii::$app->response->cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => Yii::$app->session->get('language'),
                    'expire' => time() + 31536000 // one year
                ]));
            } elseif ($language = Yii::$app->session->get('language')) {

                Yii::$app->language = $this->getLanguageCode($language);
            } elseif (isset(Yii::$app->request->cookies['language'])) {

                $language = Yii::$app->request->cookies['language']->value;
                Yii::$app->language = $this->getLanguageCode($language);
            }

            Yii::$app->formatter->locale = Yii::$app->language;
        }

        return $event->isValid;
    }