/**
  * @return boolean whether the record should be deleted. Defaults to true.
  * @throws InvalidParamException
  */
 public function beforeDelete()
 {
     if (LanguageHelper::removeAllData($this)) {
         return parent::beforeDelete();
     }
     return false;
 }
 public function up()
 {
     $this->insert('{{%language}}', ['id' => 1, 'name' => 'Việt Nam', 'code' => 'vi', 'country' => 'vn', 'status' => 1]);
     $this->insert('{{%language}}', ['id' => 2, 'name' => 'United States', 'code' => 'en', 'country' => 'us', 'status' => 1]);
     $this->insert('{{%phrase}}', ['id' => 1, 'name' => 'language']);
     $this->insert('{{%phrase}}', ['id' => 2, 'name' => 'phrase']);
     $this->insert('{{%phrase}}', ['id' => 3, 'name' => 'name']);
     $this->insert('{{%phrase}}', ['id' => 4, 'name' => 'code']);
     $this->insert('{{%phrase}}', ['id' => 5, 'name' => 'country']);
     $this->insert('{{%phrase}}', ['id' => 6, 'name' => 'status']);
     $this->insert('{{%phrase}}', ['id' => 7, 'name' => 'translate']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 1, 'language_id' => 1, 'value' => 'Ngôn ngữ']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 1, 'language_id' => 2, 'value' => 'Language']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 2, 'language_id' => 1, 'value' => 'Từ ngữ']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 2, 'language_id' => 2, 'value' => 'Phrase']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 3, 'language_id' => 1, 'value' => 'Tên']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 3, 'language_id' => 2, 'value' => 'Name']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 4, 'language_id' => 1, 'value' => 'Mã']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 4, 'language_id' => 2, 'value' => 'Code']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 5, 'language_id' => 1, 'value' => 'Quốc gia']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 5, 'language_id' => 2, 'value' => 'Country']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 6, 'language_id' => 1, 'value' => 'Trạng thái']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 6, 'language_id' => 2, 'value' => 'Status']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 7, 'language_id' => 1, 'value' => 'Dịch']);
     $this->insert('{{%phrase_translate}}', ['phrase_id' => 7, 'language_id' => 2, 'value' => 'Translate']);
     LanguageHelper::setLanguages();
     LanguageHelper::setAllData();
 }
Пример #3
0
 /**
  * @param $name
  *
  * @return string
  * @since 2.0.0
  */
 public function __get($name)
 {
     if (array_key_exists($name, $this->values)) {
         return $this->values[$name];
     } else {
         return LanguageHelper::newPhrase($name);
     }
 }
 /**
  * Initializes the object.
  * This method is invoked at the end of the constructor after the object is initialized with the
  * given configuration.
  * @throws Exception|InvalidParamException
  */
 public function init()
 {
     parent::init();
     LanguageAsset::register($this->view);
     $this->urlManager = Yii::$app->urlManager;
     $this->languages = LanguageHelper::getLanguages();
     $this->current = ['code' => 'en', 'name' => 'United States', 'country' => 'us'];
 }
 /**
  * Attaches the behavior object to the component.
  * The default implementation will set the [[owner]] property
  * and attach event handlers as declared in [[events]].
  * Make sure you call the parent implementation if you override this method.
  *
  * @param Component $owner the component that this behavior is to be attached to.
  *
  * @throws InvalidConfigException
  */
 public function attach($owner)
 {
     /** @var ActiveRecord $ownerClassName */
     /** @var ActiveRecord $owner */
     parent::attach($owner);
     $this->module = Yii::$app->getModule('language');
     $this->currentLanguage = Yii::$app->language;
     foreach (LanguageHelper::getLanguages() as $language) {
         $this->availableLanguages[$language['code']] = $language['name'];
     }
     if (empty($this->availableLanguages) || !is_array($this->availableLanguages)) {
         throw new InvalidConfigException('Please specify at least one of available languages on ' . Url::to(['language/index']), 101);
     }
     if (array_values($this->availableLanguages) !== $this->availableLanguages) {
         $this->availableLanguages = array_keys($this->availableLanguages);
     }
     if (empty($this->attributes) || !is_array($this->attributes)) {
         throw new InvalidConfigException('Please specify multilingual attributes for the ' . get_class($this) . ' in the ' . get_class($this->owner), 103);
     }
     if (!$this->translateClassName) {
         if ($this->module->modelNamespace != null) {
             $this->translateClassName = $this->module->modelNamespace . '\\' . (new ReflectionClass($this->owner))->getShortName() . ucfirst($this->module->suffix);
         } else {
             $this->translateClassName = get_class($this->owner) . ucfirst($this->module->suffix);
         }
     }
     $this->ownerClassName = get_class($this->owner);
     $ownerClassName = $this->ownerClassName;
     $this->ownerPrimaryKey = $ownerClassName::primaryKey()[0];
     if (!$this->translateForeignKey) {
         $this->translateForeignKey = $this->owner->tableName() . '_id';
     }
     if (!$this->translateTableName) {
         $this->translateTableName = $this->owner->tableName() . '_' . $this->module->suffix;
     }
     $rules = $owner->rules();
     $validators = $owner->getValidators();
     foreach ($rules as $rule) {
         if ($rule[1] == 'unique') {
             continue;
         }
         $rule_attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
         $attributes = array_intersect($this->attributes, $rule_attributes);
         if (empty($attributes)) {
             continue;
         }
         $rule_attributes = [];
         foreach ($attributes as $key => $attribute) {
             foreach ($this->availableLanguages as $language) {
                 $rule_attributes[] = $attribute . "_" . $language;
             }
         }
         $params = array_slice($rule, 2);
         if ($rule[1] !== 'required') {
             $validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params);
         } else {
             $validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params);
         }
     }
     if (class_exists($this->translateClassName)) {
         $translation = new $this->translateClassName();
         foreach ($this->availableLanguages as $language) {
             foreach ($this->attributes as $attribute) {
                 $this->setTranslateAttribute($attribute . "_" . $language, $translation->{$attribute});
                 if ($language == Yii::$app->language) {
                     $this->setTranslateAttribute($attribute, $translation->{$attribute});
                 }
             }
         }
     }
 }