'eng', 'ru' => 'rus', 'uk' => 'ukr', ); public static $db2jsMap = array( 'eng' => 'en', 'rus' => 'ru', 'ukr' => 'uk', ); /** * @throws \yii\base\InvalidConfigException * @return string eng|ukr|rus */ public static function getLanguage() { if (($languageId = ArrayHelper::getValue($_COOKIE, 'language')) && isset(self::$db2jsMap[$languageId])) { self::setLanguage($languageId); } else { if (null === ($languageId = Yii::$app->getSession()->get('languageId')) || empty(self::$db2jsMap[$languageId])) { if (Yii::$app->getDb()->getIsActive() && Yii::$app->user->identity && ($preferredLanguage = Yii::$app->user->identity->preferredLanguage) && array_key_exists($preferredLanguage, self::$db2jsMap) ) { $languageId = $preferredLanguage; self::setLanguage($languageId); } else { $languageId = self::parseBrowserLanguage(); } } } // if (null === ($languageId = Yii::$app->getSession()->get('languageId')) || empty(self::$db2jsMap[$languageId])) { // if (Yii::$app->getDb()->getIsActive() // && Yii::$app->user->identity // && ($preferredLanguage = Yii::$app->user->identity->preferredLanguage) // && array_key_exists($preferredLanguage, self::$db2jsMap) // ) { // $languageId = $preferredLanguage; // self::setLanguage($languageId); // } else if (null == ($languageId = ArrayHelper::getValue($_COOKIE, 'language')) || empty(self::$db2jsMap[$languageId])) { // $languageId = self::parseBrowserLanguage(); // } // } return $languageId && isset(self::$db2jsMap[$languageId]) ? $languageId : self::getDefaultLanguage(); } /** * @param $languageId string eng|ukr|rus * * @throws \yii\base\InvalidParamException * @throws \yii\base\InvalidConfigException */ public static function setLanguage($languageId) { if (null === $languageId || !array_key_exists($languageId, self::$db2jsMap)) { $languageId = self::parseBrowserLanguage(); } Yii::$app->getSession()->set('languageId', $languageId); Users::setLanguage($languageId); unset($_COOKIE['language']); setcookie('language', null, -1, '/'); } /** * @throws \yii\base\InvalidParamException * @throws \yii\base\InvalidConfigException * @return string eng|ukr|rus */ public static function detectLanguage() { if (null == ($languageId = ArrayHelper::getValue($_COOKIE, 'language')) && null == ($languageId = Yii::$app->getSession()->get('languageId')) ) { $languageId = self::parseBrowserLanguage(); } // if (null == ($languageId = Yii::$app->getSession()->get('languageId')) // && null == ($languageId = ArrayHelper::getValue($_COOKIE, 'language')) // ) { // $languageId = self::parseBrowserLanguage(); // } return $languageId && isset(self::$db2jsMap[$languageId]) ? $languageId : self::getDefaultLanguage(); } /** * @return string eng|ukr|rus */ public static function getDefaultLanguage() { reset(self::$db2jsMap); return key(self::$db2jsMap); } /** * @return string eng|ukr|rus */ public static function parseBrowserLanguage() { $httpAccept = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; $language = null; if (isset($httpAccept) && strlen($httpAccept) > 1) { // Split possible languages into array $httpAcceptArray = explode(",", $httpAccept); $lang = array(); foreach ($httpAcceptArray as $val) { // check for q-value and create associative array. No q-value means 1 by rule if (preg_match("/(.*);q=([0-1]{0,1}\.\d{0,4})/i", $val, $matches)) { $lang[$matches[1]] = (float)$matches[2]; } else { $lang[$val] = 1.0; } } // return default language (highest q-value) $qValues = 0.0; foreach ($lang as $key => $value) { if ($value > $qValues) { $qValues = (float)$value; $language = $key; } } } $code = $language ? substr(strtolower($language), 0, 2) : null; return $language && isset(self::$js2dbMap[$code]) ? self::$js2dbMap[$code] : self::getDefaultLanguage(); } }