The database must contain the following two tables: source_message and message.
The source_message table stores the messages to be translated, and the message table stores
the translated messages. The name of these two tables can be customized by setting [[sourceMessageTable]]
and [[messageTable]], respectively.
The database connection is specified by [[db]]. Database schema could be initialized by applying migration:
yii migrate --migrationPath=@yii/i18n/migrations/
If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.
/** * Initializes the DbMessageSource component. */ public function init() { parent::init(); if ($this->autoInsert) { $this->on(static::EVENT_MISSING_TRANSLATION, function ($event) { if (!isset($this->messagesId[$event->message])) { $query = new Query(); $id = $query->select("id")->from($this->sourceMessageTable)->where(['category' => $event->category, 'message' => $event->message])->scalar($this->db); if ($id === false) { $this->db->createCommand()->insert($this->sourceMessageTable, ['category' => $event->category, 'message' => $event->message])->execute(); $id = $this->db->lastInsertID; } /* @var $i18n I18N */ $i18n = Yii::$app->i18n; $languages = $i18n->getLanguages(); foreach ($languages as $language_id => $language) { $query = new Query(); $exists = $query->from($this->messageTable)->where(['id' => $id, 'language_id' => $language_id])->exists($this->db); if (!$exists) { $this->db->createCommand()->insert($this->messageTable, ['id' => $id, 'language_id' => $language_id, 'translation' => ''])->execute(); } } $this->messagesId[$event->message] = $id; } $event->translatedMessage = $event->message; }); } }
/** * @throws InvalidConfigException */ public function init() { if (!$this->languages) { throw new InvalidConfigException('You should configure i18n component [language]'); } if (empty($this->defaultLanguage)) { $this->defaultLanguage = Yii::$app->language; } if (empty($this->languageSessionKey)) { $this->languageSessionKey = 'language'; } if (empty($this->languageParam)) { $this->languageParam = 'language'; } if (!isset($this->translations['*'])) { $this->translations['*'] = ['class' => DbMessageSource::className(), 'sourceMessageTable' => $this->sourceMessageTable, 'messageTable' => $this->messageTable, 'on missingTranslation' => $this->missingTranslationHandler]; } if (!isset($this->translations['app']) && !isset($this->translations['app*'])) { $this->translations['app'] = ['class' => DbMessageSource::className(), 'sourceMessageTable' => $this->sourceMessageTable, 'messageTable' => $this->messageTable, 'on missingTranslation' => $this->missingTranslationHandler]; } if ($this->autoSetLanguage) { $this->setLanguage(); } else { \Yii::$app->language = \Yii::$app->session->get($this->languageSessionKey, \Yii::$app->language); } parent::init(); }
/** * @return array */ protected function getMessageSourceConfig() { $defaults = ['class' => DbMessageSource::className()]; if ($this->handleMissing) { $defaults['on missingTranslation'] = ['metalguardian\\i18n\\components\\TranslationEventHandler', 'handleMissingTranslation']; } return ArrayHelper::merge($defaults, $this->messageSourceConfig); }
/** * @throws InvalidConfigException */ public function init() { if (!$this->languages) { throw new InvalidConfigException('You should configure i18n component [language]'); } if (!isset($this->translations['*'])) { $this->translations['*'] = ['class' => DbMessageSource::className(), 'db' => $this->db, 'sourceMessageTable' => $this->sourceMessageTable, 'messageTable' => $this->messageTable, 'on missingTranslation' => $this->missingTranslationHandler]; } if (!isset($this->translations['app']) && !isset($this->translations['app*'])) { $this->translations['app'] = ['class' => DbMessageSource::className(), 'db' => $this->db, 'sourceMessageTable' => $this->sourceMessageTable, 'messageTable' => $this->messageTable, 'on missingTranslation' => $this->missingTranslationHandler]; } parent::init(); }
/** * @throws InvalidConfigException */ public function init() { if (!$this->languages) { throw new InvalidConfigException('You should configure i18n component [language]'); } if ($this->languages instanceof \Closure) { $this->languages = $this->languages->__invoke(); } $cacheConfig = []; if ($this->enableCaching) { $this->cache = Instance::ensure($this->cache, Cache::className()); $cacheConfig = ['cache' => $this->cache, 'cachingDuration' => $this->cachingDuration, 'enableCaching' => true]; } $translationConfig = ['class' => DbMessageSource::className(), 'db' => $this->db, 'sourceMessageTable' => $this->sourceMessageTable, 'messageTable' => $this->messageTable, 'on missingTranslation' => $this->missingTranslationHandler]; $translationConfig = ArrayHelper::merge($translationConfig, $cacheConfig); if (!isset($this->translations['*'])) { $this->translations['*'] = $translationConfig; } if (!isset($this->translations['app']) && !isset($this->translations['app*'])) { $this->translations['app'] = $translationConfig; } parent::init(); }