/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = SourceMessage::find()->joinWith(['messages']); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'category', $this->category])->andFilterWhere(['like', 'message', $this->message]); $translations = array_filter((array) $this->translation); $or = ['or']; foreach ($translations as $key => $text) { $or[] = ['and', ['message.language' => $key], ['or like', 'message.translation', $text]]; } $query->andWhere($or); return $dataProvider; }
/** * @param string $category * @param string $message * @return null|SourceMessage */ public static function get($category, $message) { $driver = Yii::$app->getDb()->getDriverName(); $condition = $driver === 'mysql' ? '= BINARY' : '='; $sourceMessage = SourceMessage::find()->where(['category' => $category])->andWhere([$condition, 'message', $message])->one(); return $sourceMessage; }
/** * @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); } } }