如何本地化Bean验证消息 [英] How to localize Bean Validation messages
问题描述
我有一个类,并使用注释来验证类属性.
I have a class and using annotation to validate the class properties.
我的网页(jsf2.2/primefaces/maven)是多语言的(DE-utch,FR-rench,IT-alian).
My web page (jsf2.2/primefaces/maven) is multilanguage (DE-utch, FR-rench, IT-alian).
我的问题是,休眠验证器支持某些语言(de,en,es,fr,hu,tr,pt_BR,mn_MN,zn_CN),但不支持意大利语! (在jar中列出,org-> hibernate-> validator) 因此,当我使用网页的意大利语版本时,验证消息将以英文显示(默认为休眠状态).
My problem is that the hibernate-validator has support for some languages(de, en, es, fr, hu, tr, pt_BR, mn_MN, zn_CN) but not Italian! (listed in the jar, org->hibernate->validator) So when i use the Italian version of my web page the validation messages are shown in english(default choice of hibernate).
我该如何覆盖它,并提供自定义的意大利语验证消息?
How i can override this, and provide custom italian validation messages?
我不想弄乱休眠的jar,因为该项目是行家,并且该jar是自动获取的.
I don't want to mess with the hibernate jar, because the project is maven and the jar is automatically fetched.
推荐答案
设法克服了这一难题,而又没有弄乱休眠的jar.
Managed to overcome this without messing with the hibernate jar.
如果我们要覆盖默认的ValidationMessages.properties
:
If we want to override default ValidationMessages.properties
:
- 我们可以选择适当的文件(提示:从休眠模式复制它们
罐子:
ValidationMessages.properties
,ValidationMessages_de.properties
,ValidationMessages_fr.properties
)并进行相应的编辑. - 将所有内容全部放入我们项目的
src/main/resources
文件夹中.所以 该文件夹包含所有* .properties文件. - *提供对更多语言的验证(
休眠的默认语言),f.e.意大利语,我们创建了一个
ValidationMessages_it.properties
并将其放在src/main/resources
文件夹中. (src/main/resources/ValidationMessages_it.properties
).
- We can pick the appropriate files (hint:copy them from the hibernate
jar:
ValidationMessages.properties
,ValidationMessages_de.properties
,ValidationMessages_fr.properties
) and edit them accordingly. - Put all all in our project's
src/main/resources
folder. So the folder contains all the *.properties files. - *To provide validations for more languages (except of
hibernate's default langs), f.e. the italian language, we create a
ValidationMessages_it.properties
and put it atsrc/main/resources
folder. (src/main/resources/ValidationMessages_it.properties
).
毕竟,当将语言环境设置为意大利语时,如果有任何覆盖的/其他验证消息(例如ValidationMessages _ *.properties),hibernate将首先在后一个文件夹中使用它们,而不是使用默认消息!
After all, when locale is set to italian language, hibernate will look first at latter folder, in case of any overridden/other validation messages (like ValidationMessages_*.properties), and use them instead of default ones!
Caution This solution is for MAVEN project, otherwise you should put *properties.files at
/src/main/java
folder, as mentioned in another similar question comment
这篇关于如何本地化Bean验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!