如何本地化Bean验证消息 [英] How to localize Bean Validation messages

查看:86
本文介绍了如何本地化Bean验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,并使用注释来验证类属性.

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:

  1. 我们可以选择适当的文件(提示:从休眠模式复制它们 罐子:ValidationMessages.propertiesValidationMessages_de.propertiesValidationMessages_fr.properties)并进行相应的编辑.
  2. 将所有内容全部放入我们项目的 src/main/resources 文件夹中.所以 该文件夹包含所有* .properties文件.
  3. *提供对更多语言的验证( 休眠的默认语言),f.e.意大利语,我们创建了一个 ValidationMessages_it.properties并将其放在 src/main/resources 文件夹中. ( src/main/resources/ValidationMessages_it.properties ).
  1. 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.
  2. Put all all in our project's src/main/resources folder. So the folder contains all the *.properties files.
  3. *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 at src/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!

警告 此解决方案适用于 MAVEN 项目,否则,应将* properties.files放在

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆