class MultilingualBehavior extends Behavior
{
// другой код класа
public function attach($owner)
{
/** @var ActiveRecord $owner */
parent::attach($owner);
if (empty($this->languages) || !is_array($this->languages)) {
throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the '
. get_class($this->owner) . ' or in the application parameters', 101);
}
if (array_values($this->languages) !== $this->languages) { //associative array
$this->languages = array_keys($this->languages);
}
$this->languages = array_unique(array_map(function ($language) {
return $this->getLanguageBaseName($language);
}, $this->languages));
if (!$this->defaultLanguage) {
$this->defaultLanguage = isset(Yii::$app->params['defaultLanguage']) && Yii::$app->params['defaultLanguage'] ?
Yii::$app->params['defaultLanguage'] : Yii::$app->language;
}
$this->defaultLanguage = $this->getLanguageBaseName($this->defaultLanguage);
if (!$this->currentLanguage) {
$this->currentLanguage = $this->getLanguageBaseName(Yii::$app->language);
}
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->langClassName) {
$this->langClassName = get_class($this->owner) . 'Lang';
}
$this->langClassShortName = $this->getShortClassName($this->langClassName);
$this->ownerClassName = get_class($this->owner);
$this->ownerClassShortName = $this->getShortClassName($this->ownerClassName);
/** @var ActiveRecord $className */
$className = $this->ownerClassName;
$this->ownerPrimaryKey = $className::primaryKey()[0];
if (!isset($this->langForeignKey)) {
throw new InvalidConfigException('Please specify langForeignKey for the ' . get_class($this) . ' in the '
. get_class($this->owner), 105);
}
$rules = $owner->rules();
$validators = $owner->getValidators();
foreach ($rules as $rule) {
if (in_array($rule[1], $this->excludedValidators))
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->languages as $language)
if ($language != $this->defaultLanguage)
$rule_attributes[] = $this->getAttributeName($attribute, $language);
}
if (isset($rule['skipOnEmpty']) && !$rule['skipOnEmpty'])
$rule['skipOnEmpty'] = !$this->requireTranslations;
$params = array_slice($rule, 2);
if ($rule[1] !== 'required' || $this->requireTranslations) {
$validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params);
} elseif ($rule[1] === 'required') {
$validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params);
}
}
if ($this->dynamicLangClass) {
$this->createLangClass();
}
$translation = new $this->langClassName;
foreach ($this->languages as $lang) {
foreach ($this->attributes as $attribute) {
$attributeName = $this->localizedPrefix . $attribute;
$this->setLangAttribute($this->getAttributeName($attribute, $lang), $translation->{$attributeName}); // ошибка здесь
if ($lang == $this->defaultLanguage) {
$this->setLangAttribute($attribute, $translation->{$attributeName});
}
}
}
}
//...
}