Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customize Yii2 english validations messages translations?

I want to change english validation messages in overall app.

so for example:

showing "field required" rather than "date of birth is required".

I tried to configure the I18N component as following:

'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@common/messages',
                    'sourceLanguage' => 'en',
                    'fileMap' => [
                        'yii' => 'yii.php',
                    ]
                    // 'on missingTranslation' => ['app\components\TranslationEventHandler', 'handleMissingTranslation']
                ],
            ],

but not working

like image 238
Downy Avatar asked Oct 23 '15 00:10

Downy


2 Answers

To change language globally for the whole application, add language to your config:

return [
    ...
    'language' => 'ru',
    ...
],

This is for russian language, you can set any language you want. But framework messages are translated only in popular languages, you can open vendor/yiisoft/yii2/messages folder to see the complete list. Also you can refer to IETF language tags page.

To change message just for specific validation rule use message option:

['attributeName', 'validatorName', 'message' => 'Your customized message'],

Note that you can use internationalization (Yii::t()) here if your app is multilanguage.

like image 57
arogachev Avatar answered Nov 17 '22 09:11

arogachev


['attribute', 'validator', 'message' => 'Your message {attribute}'],
like image 1
Abdallah Awwad Alkhwaldah Avatar answered Nov 17 '22 08:11

Abdallah Awwad Alkhwaldah