Alex
Alex

Reputation: 3

Yii2 - i18n for the {attribute} in js validation messages

After changing the default language, the js validation messages are also changed to the target language. However the attribute names are still in English, e.g. {attribute}(in English) XXXXXXXX(in target language). My question is: how can I translate those attribute names to the target language as well?

Thanks!

Upvotes: 0

Views: 787

Answers (1)

arogachev
arogachev

Reputation: 33548

There is special method in model called attributeLabels() for these purposes. It returns the array where keys are original attribute names and values - according labels. You should fill this manually in dependence of used language and desired output.

Example for russian language:

public function attributeLabels()
{
    return [
        'city_id' => 'Город',
    ];
}

You can use i18n here as well:

public function attributeLabels()
{
    return [
        'city_id' => \Yii::t('app', 'City');,
    ];
}

Default validation uses this labels for displaying errors.

Upvotes: 1

Related Questions