public function run() { $items = []; $preContent = ''; $isPost = \Yii::$app->request->isPost; $defaultLang = Lang::getLang()->code; foreach ($this->models as $k => $model) { $preContent .= $isPost && !$model->validate() ? '<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><h4>' . $model->langModel->title . '</h4>' . Html::errorSummary($model) . '</div>' : ''; $content = $this->view->render($this->template, ['model' => $model, 'k' => $k, 'form' => $this->form]); $items[] = ['label' => $model->langModel->title, 'content' => $content, 'active' => $model->lang == $defaultLang]; } return $preContent . Tabs::widget(['items' => $items]); }
public static function t($category, $t, $resetCategory = false) { if (empty($t)) { return 'Empty text'; } if (empty(self::$t[$category]) || $resetCategory) { $categoryList = self::find()->where(['lang' => \Yii::$app->language])->all(); self::$t[$category] = []; foreach ($categoryList as $categoryItem) { self::$t[$categoryItem->category][$categoryItem->alias] = $categoryItem->t; } $resetCategory = false; } $t = trim($t); $alias = md5(gzcompress($t)); if (!empty(self::$t[$category][$alias])) { return self::$t[$category][$alias]; } $model = new Language(); $model->id = $model->nextId; $model->lang = Lang::getLang()->code; $model->alias = $alias; $model->category = $category; $model->t = $t; $model->create(); return self::t($category, $t, true); }
/** * Creates a new Page model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { /** * @var $model Model */ $class = $this->modelClass; $model = new $class(); $model->id = $model->nextId; $model->lang = Lang::getLang()->code; $this->modelSetGetVariable($model); if (in_array('position', $model->attributes())) { $model->position = $model->positionNext; } $models = [$model]; if (\Yii::$app->request->isAjax && $class::loadMultiple($models, $_POST)) { \Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models); } if ($class::loadMultiple($models, $_POST) && $class::validateMultiple($models)) { $model->create(); return $this->redirect($this->generateRedirectParams($model, $this->redirectCreate)); } else { $renderMethod = \Yii::$app->request->get('partial') ? 'renderPartial' : 'render'; $renderMethod = \Yii::$app->request->isAjax ? 'renderPartial' : $renderMethod; return $this->{$renderMethod}('create', ['models' => $models, 'model' => $model]); } }
/** * Finds the Lang model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Lang the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Lang::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function lang($code = null) { $class = $this->modelClass; $lang = Lang::getLang($code); if (isset($lang)) { $this->andWhere([$class::tableName() . '.lang' => $lang->code]); } return $this; }
public function bootstrap($app) { $app->on(Application::EVENT_BEFORE_ACTION, function () { $langCode = \Yii::$app->request->get('lang'); $langModel = Lang::getLang($langCode); \Yii::$app->language = $langModel->code; \Yii::$app->sourceLanguage = $langModel->locale; }); }
public static function to($url = '', $scheme = false) { if (is_array($url)) { $lang = Lang::getLang(); if ($lang->code != \Yii::$app->language) { $url['lang'] = \Yii::$app->language; } } return parent::to($url, $scheme); }
public function run() { $default = Lang::getLang()->code; $currentCode = \Yii::$app->language; foreach ($this->links as &$link) { if (is_array($link) && $default != $currentCode) { $link['url']['lang'] = $currentCode; } } return parent::run(); }
public function setLanguage() { $langCode = \Yii::$app->request->get('lang'); $langModel = null; if ($langCode) { $langModel = Lang::findOne(['code' => $langCode]); } if (!$langModel) { $langModel = Lang::getDefault(); } \Yii::$app->language = $langModel->code; \Yii::$app->sourceLanguage = $langModel->locale; }
/** * Creates a new Page model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $models = []; $langList = Lang::find()->orderBy(['default' => SORT_DESC])->all(); foreach ($langList as $lang) { $models[$lang->code] = new Page(); $models[$lang->code]->lang = $lang->code; $models[$lang->code]->id = $models[$lang->code]->nextId; } if (Page::loadMultiple($models, \Yii::$app->request->post()) && Page::validateMultiple($models)) { foreach ($models as $model) { $model->save(); } return $this->redirect(['index']); } else { return $this->render('create', ['models' => $models]); } }
public function getAll() { return self::find()->joinWith('langModel')->where([$this->tableName() . '.id' => $this->id])->orderBy([Lang::tableName() . '.default' => SORT_DESC])->all(); }
<h1><?php echo Html::encode($this->title); ?> </h1> <?php echo $this->render('_search', ['model' => $searchModel]); ?> <?php $form = \yii\bootstrap\ActiveForm::begin(['fieldConfig' => ['template' => '{input}{error}']]); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'id', 'headerOptions' => ['class' => 'col-md-1']], ['attribute' => 'lang', 'filter' => \app\modules\lang\model\Lang::dropdown()], ['attribute' => 'category', 'filter' => \app\modules\lang\models\Language::categoryDropdown()], ['attribute' => 't', 'value' => function ($model, $attributes, $key) use($form) { /** @var $model \app\modules\lang\models\Language */ return $form->field($model, '[' . $key . ']t')->textarea(['rows' => 1]); }, 'format' => 'raw', 'headerOptions' => ['class' => 'col-md-8']], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{delete}']]]); ?> <div class="row"> <div class="col-md-offset-10 col-md-2 text-right"> <?php echo Html::submitButton('Сохранить', ['class' => 'btn btn-success']); ?> </div> </div> <?php
echo Language::t('main', 'Контакты'); ?> </a> </li> <!-- Languages --> <li> <a href="#" class="mn-has-sub"> <?php echo \Yii::$app->language; ?> <i class="fa fa-angle-down"></i> </a> <ul class="mn-sub"> <?php foreach (Lang::getChangeData() as $url => $title) { ?> <li> <a href="<?php echo $url; ?> "><?php echo $title; ?> </a> </li> <?php } ?> </ul>
public static function getChangeData() { $uri = \Yii::$app->request->url; $langs = Lang::find()->orderBy(['default' => SORT_DESC])->all(); $preg = []; foreach ($langs as $lang) { if (!$lang->default) { $preg[] = $lang->code; } } $base = Url::base(); if ($uri == '/en') { } foreach ($langs as $lang) { $url = $lang->default ? $base . '/' : $base . '/' . $lang->code; $data[$url] = $lang->title; } return $data; $preg = '#\\/(' . implode('\\/|', $preg) . ')#i'; $cleanUrl = preg_replace($preg, '', $uri); $route = str_replace($base, '', $uri); foreach ($langs as $lang) { if ($lang->default) { $lang->url = $cleanUrl; } elseif (!preg_match($preg, $uri)) { $lang->url = $base . '/' . $lang->code . $route; } if (substr($lang->url, -1, 1) == '/') { $lang->url = substr($lang->url, 0, strlen($lang->url) - 1); } $data[$lang->url] = $lang->title; } if (YII_DEBUG) { print_r([$uri, $base, $data]); } return $data; }
use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model app\modules\lang\models\LanguageSearch */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="language-search"> <div class="well"> <?php $form = ActiveForm::begin(['action' => ['index'], 'method' => 'get', 'options' => ['class' => 'form-inline']]); ?> <?php echo $form->field($model, 'lang')->dropDownList(\app\modules\lang\model\Lang::dropdown()); ?> <?php echo $form->field($model, 'category')->dropDownList(\app\modules\lang\models\Language::categoryDropdown(), ['prompt' => '---']); ?> <?php echo $form->field($model, 't'); ?> <div class="form-group"> <?php echo Html::submitButton(Yii::t('app', 'Поиск'), ['class' => 'btn btn-primary']); ?> <?php
public function run() { $data = Lang::getChangeData(); return $this->render('LanguageChange', ['data' => $data]); }