/** * Finds the SourceMessage model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return SourceMessage the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = SourceMessage::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException(Module::t('The requested page does not exist.')); } }
/** * @return array * @throws InvalidConfigException */ public function getColumns() { $columns = [['attribute' => 'id'], ['attribute' => 'message', 'format' => 'raw', 'value' => function (SourceMessage $model) { return Html::a(Html::encode($model->message), ['update', 'id' => $model->id], ['data-pjax' => 0]); }]]; /** @var \metalguardian\i18n\components\I18n $i18n */ $i18n = Yii::$app->getI18n(); if (!$i18n instanceof I18n) { throw new InvalidConfigException(Module::t('I18n component have to be instance of metalguardian\\i18n\\components\\I18n')); } foreach ($i18n->languages as $language) { $columns[] = ['attribute' => 'translation', 'label' => Module::t('Translation[{language}]', ['language' => $language]), 'value' => function (SourceMessage $data) use($language) { return isset($data->messages[$language]) ? Html::encode($data->messages[$language]->translation) : null; }, 'filter' => Html::activeTextInput($this, 'translation[' . $language . ']', ['class' => 'form-control'])]; } $columns[] = ['attribute' => 'category', 'filter' => \yii\helpers\ArrayHelper::map(SourceMessage::getCategories(), 'category', 'category'), 'options' => ['class' => 'col-sm-1']]; return $columns; }
/** * Populate messages * * @throws InvalidConfigException */ public function populateMessages() { /** @var \metalguardian\i18n\components\I18n $i18n */ $i18n = Yii::$app->getI18n(); if (!$i18n instanceof I18n) { throw new InvalidConfigException(Module::t('I18n component have to be instance of metalguardian\\i18n\\components\\I18n')); } $messages = []; foreach ($i18n->languages as $language) { if (!isset($this->messages[$language])) { $message = new Message(); $message->language = $language; $messages[$language] = $message; } else { $messages[$language] = $this->messages[$language]; } } $this->populateRelation('messages', $messages); }
<div class="source-message-form"> <?php $form = ActiveForm::begin(); ?> <?php foreach ($model->messages as $language => $message) { ?> <?php echo $form->field($message, '[' . $language . ']translation', ['options' => ['class' => 'form-group col-sm-6']])->textarea()->label($language); ?> <?php } ?> <div class="form-group"> <?php echo Html::submitButton(Module::t('Update'), ['class' => 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div> </div>
/** * @inheritdoc */ public function attributeLabels() { return ['id' => Module::t('ID'), 'language' => Module::t('Language'), 'translation' => Module::t('Translation')]; }
<?php use metalguardian\i18n\models\SourceMessage; use metalguardian\i18n\Module; use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; /* @var $this yii\web\View */ /* @var $searchModel metalguardian\i18n\models\SourceMessageSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Module::t('Source Messages'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="source-message-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php Pjax::begin(); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => $searchModel->getColumns()]); ?> <?php Pjax::end(); ?> </div>
/** * @param string $messagePath * @param string $category * @throws Exception * @throws InvalidConfigException * @throws \yii\base\Exception */ public function actionExport($messagePath, $category = null) { $messagePath = realpath(Yii::getAlias($messagePath)); if (!is_dir($messagePath)) { throw new Exception('The message path [[' . $messagePath . ']] is not a valid directory.'); } /** @var SourceMessage[] $sourceMessages */ $sourceMessages = SourceMessage::find()->filterWhere(['category' => $category])->with(['messages'])->asArray()->all(); $messages = []; /** @var \metalguardian\i18n\components\I18n $i18n */ $i18n = Yii::$app->getI18n(); if (!$i18n instanceof I18n) { throw new InvalidConfigException(Module::t('I18n component have to be instance of metalguardian\\i18n\\components\\I18n')); } foreach ($sourceMessages as $sourceMessage) { $translations = $sourceMessage['messages']; foreach ($i18n->languages as $language) { $messages[$sourceMessage['category']][$language][$sourceMessage['message']] = isset($translations[$language]) && !empty($translations[$language]['translation']) ? $translations[$language]['translation'] : ''; } } foreach ($messages as $category => $languages) { foreach ($languages as $language => $translations) { $fileName = FileHelper::normalizePath($messagePath . '/' . $language . '/' . $category) . '.php'; if (!is_file($fileName)) { $dir = dirname($fileName); if (!FileHelper::createDirectory($dir)) { throw new Exception('Directory [[' . $dir . ']] is not created'); } } ksort($translations); $array = VarDumper::export($translations); $content = <<<EOD <?php /** * Message translations. * * This file is automatically exported from database * * NOTE: this file must be saved in UTF-8 encoding. */ return {$array}; EOD; file_put_contents($fileName, $content); $this->stdout("Translation for category [[{$category}]] for language [[{$language}]] exported to the [[{$fileName}]].\n\n", Console::FG_GREEN); } } }
/** * @inheritdoc */ public function attributeLabels() { return ['category' => Module::t('Category'), 'message' => Module::t('Message')]; }
public function getMessageTable() { $config = $this->getMessageSourceConfig(); /** @var \yii\i18n\DbMessageSource $messageSource */ $messageSource = Yii::createObject($config); if (!$messageSource instanceof \yii\i18n\DbMessageSource) { throw new \yii\base\InvalidConfigException(Module::t('I18n message source have to be instance of \\yii\\i18n\\DbMessageSource')); } return $messageSource->messageTable; }