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.
부터: 2.0
저자: resurtm (resurtm@gmail.com)
상속: extends MessageSource
 /**
  * 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;
         });
     }
 }
예제 #2
0
 /**
  * @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();
 }
예제 #3
0
 /**
  * @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);
 }
예제 #4
0
 /**
  * @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();
 }
예제 #5
-1
파일: I18N.php 프로젝트: athl64/yii2-i18n
 /**
  * @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();
 }