Esempio n. 1
0
 /**
  * @throws InvalidConfigException
  */
 public function init()
 {
     if (!isset($this->translations['*'])) {
         $this->translations['*'] = ['class' => 'yii\\i18n\\PhpMessageSource'];
     }
     parent::init();
 }
Esempio n. 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();
 }
Esempio n. 3
0
 /**
  * @inheritdoc
  */
 public function format($message, $params, $language)
 {
     if (count($params) !== 0) {
         $fixedParams = [];
         // Try to fix old placeholder formats
         foreach ($params as $param => $value) {
             if (substr($param, 0, 1) === "%" && substr($param, -1, 1) === "%" && strlen($param) > 2) {
                 // Fix: %param% style params
                 $fixedParam = str_replace("%", "", $param);
                 $fixedParams[$fixedParam] = $value;
                 $message = str_replace('%' . $fixedParam . '%', '{' . $fixedParam . '}', $message);
             } elseif (substr($param, 0, 1) == "%") {
                 // Fix: %param style params
                 $fixedParam = str_replace("%", "", $param);
                 $fixedParams[$fixedParam] = $value;
                 $message = str_replace('%' . $fixedParam, '{' . $fixedParam . '}', $message);
             } elseif (substr($param, 0, 1) === "{" && substr($param, -1, 1) === "}") {
                 // Fix: {param} style params
                 $fixedParam = str_replace(['{', '}'], "", $param);
                 $fixedParams[$fixedParam] = $value;
             } elseif (substr($param, 0, 1) === ":") {
                 // Fix: :param style params
                 $fixedParam = str_replace(':', "", $param);
                 $fixedParams[$fixedParam] = $value;
                 $message = str_replace(':' . $fixedParam, '{' . $fixedParam . '}', $message);
             } else {
                 $fixedParams[$param] = $value;
             }
         }
         return parent::format($message, $fixedParams, $language);
     }
     return parent::format($message, $params, $language);
 }
Esempio n. 4
0
 /**
  * Removes wrapper `{Lang:text}` that is returned by API
  * {@inheritdoc}
  */
 public function translate($category, $message, $params, $language)
 {
     if (stripos($message, '{lang:') !== false) {
         $message = $this->removeLegacyLangTags($message);
     }
     return parent::translate($category, $message, $params, $language);
 }
 /**
  * @throws \yii\base\InvalidConfigException
  */
 public function init()
 {
     if (!is_array($this->languages) || empty($this->languages)) {
         throw new \yii\base\InvalidConfigException('You should configure i18n component [language]');
     }
     parent::init();
 }
Esempio n. 6
0
 /**
  * @throws InvalidConfigException
  */
 public function init()
 {
     parent::init();
     if ($this->languages instanceof \Closure) {
         $this->languages = call_user_func($this->languages);
     }
     if (!is_array($this->languages)) {
         throw new InvalidConfigException('\\metalguardian\\i18n\\components\\I18n::languages have to be array.');
     }
     if (empty($this->languages)) {
         throw new InvalidConfigException('\\metalguardian\\i18n\\components\\I18n::languages have to contains at least 1 item.');
     }
     $config = $this->getMessageSourceConfig();
     if (is_array($this->only)) {
         foreach ($this->only as $item) {
             if ($this->override || !isset($this->translations[$item]) && !isset($this->translations[$item . '*'])) {
                 $this->translations[$item] = $config;
             }
         }
     } else {
         if ($this->override) {
             $this->translations = ['*' => $config];
         }
     }
 }
Esempio n. 7
0
 public function translate($category, $message, $params, $language)
 {
     if (($language == 'en' || $language == 'en-GB') && $category == 'yii') {
         $language = 'en-US';
     }
     return parent::translate($category, $message, $params, $language);
 }
Esempio n. 8
0
 public function translate($category, $message, $params, $language)
 {
     $result = parent::translate($category, $message, $params, $language);
     if ($this->autoGenerate) {
         $this->checkTranslate($category, $message, $language);
     }
     return $result;
 }
Esempio n. 9
0
 /**
  * @inheritDoc
  */
 public function init()
 {
     parent::init();
     // TODO: Change the autogenerated stub
     if (!isset($this->translations['common/' . $this->suffix]) && !isset($this->translations['*'])) {
         $this->translations['common/' . $this->suffix] = ['class' => 'yii\\i18n\\PhpMessageSource', 'basePath' => '@modules/views/' . $this->suffix . '/messages', 'fileMap' => ['common/' . $this->suffix => 'common.php']];
     }
 }
Esempio n. 10
0
 public function init()
 {
     parent::init();
     if (!isset($this->translations['platform']) && !isset($this->translations['platform*'])) {
         $this->translations['platform'] = ['class' => 'yii\\i18n\\GettextMessageSource', 'sourceLanguage' => P::$app->sourceLanguage, 'basePath' => '@platform/messages'];
     }
     if (!isset($this->translations['db']) && !isset($this->translations['db*'])) {
         $this->translations['db'] = ['class' => 'yii\\i18n\\DbMessageSource', 'sourceLanguage' => P::$app->sourceLanguage, 'sourceMessageTable' => models\MessageSource::tableName(), 'messageTable' => models\Message::tableName()];
     }
 }
Esempio n. 11
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();
 }
Esempio n. 12
0
 /**
  * https://github.com/yiisoft/yii2/issues/2519
  */
 public function testMissingTranslationEvent()
 {
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     $this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function ($event) {
     });
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     $this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
     Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function ($event) {
         if ($event->message == 'New missing translation message.') {
             $event->translatedMessage = 'TRANSLATION MISSING HERE!';
         }
     });
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     $this->assertEquals('Another missing translation message.', $this->i18n->translate('test', 'Another missing translation message.', [], 'de-DE'));
     $this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
     $this->assertEquals('TRANSLATION MISSING HERE!', $this->i18n->translate('test', 'New missing translation message.', [], 'de-DE'));
     $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
     Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
 }
Esempio n. 13
0
File: I18N.php Progetto: semnt/tp01
 public function translate($category, $message, $params, $language)
 {
     if ($category == 'yii') {
         return parent::translate($category, $message, $params, $language);
     }
     $language = substr($language, 0, 2);
     $t_message = $this->getClassMessage($category, $message, $language);
     if (!$t_message) {
         $t_message = $this->getControllerMessage($message, $language);
     }
     if (!$t_message) {
         $t_message = $this->getModuleMessage($message, $language);
     }
     if (!$t_message) {
         $t_message = $this->getApplication($message, $language);
     }
     return $this->format($t_message ?: $message, $params, $language);
 }
Esempio n. 14
0
 /**
  * This function will return the translation for the model, attribute and language passed by parameter.
  * @param \yii\db\ActiveRecord $arguments[0] the instance of the model
  * @param string $arguments[1] the attribute name
  * @param string $arguments[2] language (defaults Yii::$app->language)
  * @return string
  * @throws \yii\base\InvalidConfigException
  */
 public function translate(...$arguments)
 {
     $model = $arguments[0];
     $columnName = $arguments[1];
     $language = isset($arguments[2]) ? $arguments[2] : Yii::$app->language;
     $db = Yii::$app->get($this->db);
     if (isset($model, $columnName) and $arguments[0] instanceof ActiveRecord and $model->hasAttribute($columnName)) {
         $model->tableName;
         $result = (new Query())->select('translation')->from($this->tableName)->where(['table_name' => (string) $model->tableName(), 'column_name' => (string) $columnName, 'row_id' => (string) $model->primaryKey, 'language' => (string) $language])->scalar($db);
         if ($result === false) {
             Yii::warning("No translation found in table '{$this->tableName}'' by the following parameters: ");
         } else {
             return $result;
         }
     }
     // default implementation
     return parent::translate(...$arguments);
 }
Esempio n. 15
0
 public function translate($category, $message, $params, $language)
 {
     return parent::translate($category, $message, $params, \Yii::$app->params['themesLanguage']);
 }
Esempio n. 16
0
 /**
  * This is module uninstalling callback
  */
 public function uninstall()
 {
     \Yii::configure(\Yii::$app, ['components' => ['i18n' => ['class' => I18N::className(), 'translations' => ['*' => ['class' => 'yii\\i18n\\PhpMessageSource']]]]]);
 }
Esempio n. 17
-1
 /**
  * @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();
 }