Khalid Al-Mutawa
Khalid Al-Mutawa

Reputation: 869

Yii2 Internationalization translation not working for model validation

I have i18n set up and it's working perfectly for everything except for core validators and model attribute labels. Any ideas how to fix?

In common\models\Student.php, I setup validation rules so that the password input takes a minimum length of 5 characters, and that the phone is required:

['password', 'string', 'length' => [5]],
['phone', 'required'],

I also set up the attribute labels:

public function attributeLabels() {
    return [
        'password' => Yii::t('app', 'Password'),
        'phone' => Yii::t('app', 'Phone Number'),
    ];
}

I have also set up their respective translations in messages file (which is working for everything except for validation)

Now when I validate the model in English, I get the following:

Password should contain at least 5 characters.

Phone Number cannot be blank.

Which shows English working perfectly. However when I try Arabic, I get the following:

{attribute} يجب أن يحتوي على أكثر من {min, number} {min, plural, one{حرف} few{حروف} many{حرف}}.

Phone Number لا يمكن تركه فارغًا.

Arabic translation is being loaded from Yii2 framework files: https://github.com/yiisoft/yii2-framework/blob/master/messages/ar/yii.php

Upvotes: 1

Views: 1845

Answers (2)

Khalid Al-Mutawa
Khalid Al-Mutawa

Reputation: 869

Issue solved in https://github.com/yiisoft/yii2/issues/8173

Needed to override pre-defined categories and an issue with Arabic language

Upvotes: 0

Jørgen
Jørgen

Reputation: 3567

You can do this:

['password', 'string', 'length' => [5], 'message' => Yii::t('app', 'Password must be over 5 characters long')],
['phone', 'required', 'message' => Yii::t('app', 'phone is required')],

Upvotes: 0

Related Questions