/** * Tries to determine ID of the current user's language by using cookies or browser language * @return integer|null Null - could not determine the language */ public static function run() { // We are looking for language in the user's cookie if (isset($_COOKIE['x-language-id'])) { $isset = LangHelper::getLanguageByParam('id', $_COOKIE['x-language-id']); if ($isset !== null) { return $isset['id']; } } // Let's look at the language of the browser if (getenv('HTTP_ACCEPT_LANGUAGE')) { $arr = explode(';', getenv('HTTP_ACCEPT_LANGUAGE')); foreach ($arr as $val) { $val = preg_replace("/^q=[\\d\\.]+,?/", '', $val); $isset = LangHelper::getLanguageByParam('locale', substr($val, 0, 2)); if ($isset !== null) { return $isset['id']; } } } // Default language $lang_id = LangHelper::getLanguage('id'); if ($lang_id !== null) { return $lang_id; } }
public function run($id, $lang_id = null) { $controller = $this->controller; $modelName = $controller::mlConf('model'); $contentModelName = $controller::mlConf('contentModel'); if (!$lang_id) { $lang_id = LangHelper::getLanguage('id'); } $model = $controller->findModel($id, $lang_id); if (!$model->content) { $model->populateRelation('content', new $contentModelName()); $model->content->lang_id = $lang_id; $model->content->parent_id = $model->id; } if ($model->load(Yii::$app->request->post()) && $model->content->load(Yii::$app->request->post())) { $isSaveSuccess = false; $transaction = Yii::$app->db->beginTransaction(); try { $model->save(); $model->content->save(); $transaction->commit(); $isSaveSuccess = true; } catch (\Exception $e) { $transaction->rollBack(); throw $e; } if ($isSaveSuccess) { // We will redirect to view action if it exists. Otherwise, to update $redirectAction = $this->isControllerHasViewAction() ? 'view' : 'update'; return $controller->redirect([$redirectAction, 'id' => $model->id, 'lang_id' => $lang_id]); } } return $controller->render('update', ['model' => $model, 'modelContent' => $model->content]); }
public function run() { $controller = $this->controller; $modelName = $controller::mlConf('model'); $contentModelName = $controller::mlConf('contentModel'); $model = new $modelName(); $model->populateRelation('content', new $contentModelName()); $model->content->lang_id = LangHelper::getLanguage('id'); if ($model->load(Yii::$app->request->post()) && $model->content->load(Yii::$app->request->post())) { $isSaveSuccess = false; $transaction = Yii::$app->db->beginTransaction(); try { $res = $model->save(); if ($res) { $model->content->parent_id = $model->id; $model->content->save(); $transaction->commit(); $isSaveSuccess = true; } else { throw new \Exception('Не удалось сохранить модель - ошибка валидации'); } } catch (\Exception $e) { $transaction->rollBack(); //throw $e; } if ($isSaveSuccess) { // We will redirect to view action if it exists. Otherwise, to update $redirectAction = $this->isControllerHasViewAction() ? 'view' : 'update'; return $controller->redirect([$redirectAction, 'id' => $model->id, 'lang_id' => $model->content->lang_id]); } } return $controller->render('create', ['model' => $model]); }
/** * Returns current link language * @return array|null */ public static function detectLinkLang() { $languageInurl = Yii::$app->request->languageInurl; if ($languageInurl) { $current = LangHelper::getLanguageByParam('url', $languageInurl); } else { $current = LangHelper::getLanguageByParam('default', true); } return $current; }
public function getDataCellValue($model, $key, $index) { $list = LangHelper::languages(); $items = []; foreach ($list as $lang) { $id = $lang['id']; $isContentIsset = $this->issetContent($model, $lang['id']); $text = $this->useShortNames ? $lang['locale'] : $lang['name']; $options = ['class' => $isContentIsset ? $this->linkActiveClass : $this->linkInactiveClass]; $link = [$this->useAction, 'id' => $model->id, 'lang_id' => $lang['id']]; $items[] = Html::a($text, $link, $options); } return implode(' ', $items); }
public function createUrl($params) { // Url's language such as "en" $url_lang = Yii::$app->language; // If isset lang param if (isset($params['x-language-url'])) { // Do not change url if ($params['x-language-url'] === false) { unset($params['x-language-url']); return parent::createUrl($params); } // If exists language $xLang = LangHelper::getLanguageByParam('url', $params['x-language-url']); if ($xLang) { $url_lang = $xLang['url']; } } $current = LangHelper::getLanguageByParam('locale', $url_lang); $isDefault = isset($current['default']) && $current['default']; // Delete if is pretty url if ($this->enablePrettyUrl || $isDefault) { unset($params['x-language-url']); } $url = parent::createUrl($params); if ($current) { // Is curent language // Pretty url if ($this->enablePrettyUrl && !$this->showScriptName) { $pattern = "/^" . preg_quote($this->baseUrl, '/') . "/"; $replaceTo = $this->baseUrl; if (!$isDefault) { $replaceTo .= '/' . $url_lang; } $url = preg_replace($pattern, $replaceTo, $url); } else { if ($this->enablePrettyUrl && $this->showScriptName) { // Pretty url with showScriptName $pattern = "/^" . preg_quote($this->scriptUrl, '/') . "/"; $replaceTo = $this->baseUrl; if (!$isDefault) { $replaceTo .= '/' . $url_lang; } $url = preg_replace($pattern, $replaceTo, $url); } } } return $url; }
public function getPathInfo() { $pathInfo = parent::getPathInfo(); $pattern = LangHelper::pattern(); if (preg_match("/^({$pattern})\\/(.*)/", $pathInfo, $arr)) { $this->lang_url = $arr[1]; $pathInfo = $arr[count($arr) - 1]; } else { if (isset($this->queryParams['x-language-url']) && LangHelper::getLanguageByParam('url', $this->queryParams['x-language-url'])) { // If enablePrettyUrl===false $this->lang_url = $this->queryParams['x-language-url']; } else { $this->lang_url = null; } } return $pathInfo; }