5

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

Downy
  • 91
  • 2
  • 6
  • having problem understanding your problem. Use some example or anything or include screenshot of any page you can. Do you want to change the language or you want to customize the message? – Mike Ross Oct 23 '15 at 02:46

2 Answers2

5

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.

arogachev
  • 33,150
  • 7
  • 114
  • 117
1
['attribute', 'validator', 'message' => 'Your message {attribute}'],
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Machavity Apr 29 '20 at 23:16