/** * 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}); } } } } }