覆盖Play 2.0的默认i18n消息 [英] Overriding default i18n messages for play 2.0
问题描述
我们使用@ Constraint.Required来验证表单字段,这会生成ValidationError-很好.该错误具有标准错误消息(error.required),该消息与以下字符串相关联:此字段为必填字段".
We use @Constraint.Required to validate the fields of our forms, and this generates a ValidationError - which is all well and nice. This error has a standard error message (error.required) which is associated with the following string: "This field is required".
但是,我们希望控制该字符串并(可能)覆盖它-我们如何做到这一点?我们在conf/文件夹中有一个消息文件,其中包含以下行:
However, we want to have control over this string and (possibly) override it - how can we do this? We have a message file in our conf/-folder which has the following line:
error.required = dummytext
但是即使读取了消息文件(我们进行了一些测试),错误消息仍然显示此字段是必需的".
But the error message still reads "this field is required" - even though the messages-file is being read (we did some tests).
我们如何覆盖这些消息?
How can we override these messages?
谢谢!
推荐答案
这不是一个很好的解决方案,尤其是当您要制作多语言应用程序时.因此,在这种情况下,将更改后的消息放入批注中将会失败.
That's not quite good solution especially when you want to make multilingual app. So placing changed messages in annotations will fail in such case.
幸运的是,有解决方案,它可以按预期工作.您需要将标签放入语言文件中,并以语言代码作为扩展名. conf/messages.en
,即使仅定义了一种语言,也就是英语.
Fortunately there is solution, which works as expected. You need to place your labels into the language file with language code as an extension ie. conf/messages.en
, even if there is only one language defined and it's English.
The default messages with their keys can be copied from the Play's sources
这篇关于覆盖Play 2.0的默认i18n消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!