如何在 Symfony 2.1 应用程序中全局覆盖NotBlank
约束消息?This value should not be blank
我已经尝试过使用validators.en.yml
文件,但这只有在我将validation.yml
文件中的每个 NotBlank 约束设置为常量时才有效not.blank.field
- 但修改太多。
如何在 Symfony 2.1 应用程序中全局覆盖NotBlank
约束消息?This value should not be blank
我已经尝试过使用validators.en.yml
文件,但这只有在我将validation.yml
文件中的每个 NotBlank 约束设置为常量时才有效not.blank.field
- 但修改太多。
要替换在应用程序级别触发约束This value should not be blank
时显示的消息,只需在正确的位置创建正确的翻译文件。NotBlank
为简单起见,您的语言环境似乎是英语 (en),因此创建文件app/Resources/translations/validators.en.yml
并在其中写入以下行:
# app/Resources/translations/validators.en.yml
This value should not be blank.: Y U NO SET THE VALUE?!
第一部分必须与您要翻译的消息完全匹配(在您的情况下This value should not be blank.
)。
如果您有自定义文件/文件夹层次结构,只需在<kernel root directory>/Resources/translations
.
如果您想对另一种语言(例如法语)具有相同的行为,只需在同一文件夹中创建相应的文件:
# app/Resources/translations/validators.fr.yml
This value should not be blank.: La valeur doit etre specifiee.
添加新文件后不要忘记清除缓存!
为什么它有效?因为 Symfony2 按位置优先翻译文件:http: //symfony.com/doc/current/book/translation.html#translation-locations-and-naming-conventions