Hibernate 验证和本地化错误消息?
hibernate 和我想为 hibernate 注释提供本地化错误消息 所以我创建了属性文件 ValidatorMessages.properties、ValidatorMessages_ar.properties
并将它们放入资源文件夹中,我使用 messageSource 从属性文件中读取:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
<value>classpath:errors</value>
<value>classpath:app</value>
<value>classpath:ValidatorMessages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
在类中我使用类似的东西:
@NotNull(message = "{validation.notEmpty.password}")
private string password;
在调用验证器时我使用:
Validator validator = Validation.buildDefaultValidatorFactory()
.getValidator();
Set<ConstraintViolation<MyClass> cvs = validator
.validate(myObject);
for (ConstraintViolation<MyClass> cv : cvs) {
String field = cv.getPropertyPath().toString();
result.addError(new FieldError("version", field, cv.getMessage()));
}
if (result.hasErrors()) {
initModel();
result.reject("add.version.errors");
return "manageVersions";
}
它有效英语很好,它可以正确显示英语消息,但是当切换到阿拉伯语时,它仍然显示英语消息而不是阿拉伯语,尽管
LocaleContextHolder.getLocale()
表示语言已更改并且是阿拉伯语,那么配置中是否缺少某些内容或可能导致这种情况的任何想法?
hibernate and i want to provide localized error messages for hibernate annotations
so i created to properties files ValidatorMessages.properties, ValidatorMessages_ar.properties
and put them in resources folder, and i am using messageSource to read from property files:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
<value>classpath:errors</value>
<value>classpath:app</value>
<value>classpath:ValidatorMessages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
and in the class i use something like:
@NotNull(message = "{validation.notEmpty.password}")
private string password;
and when calling the validator i use:
Validator validator = Validation.buildDefaultValidatorFactory()
.getValidator();
Set<ConstraintViolation<MyClass> cvs = validator
.validate(myObject);
for (ConstraintViolation<MyClass> cv : cvs) {
String field = cv.getPropertyPath().toString();
result.addError(new FieldError("version", field, cv.getMessage()));
}
if (result.hasErrors()) {
initModel();
result.reject("add.version.errors");
return "manageVersions";
}
it works fine with english, it displays english messages correctly, but when switching to arabic it still displays the english messages instead of arabic, although that
LocaleContextHolder.getLocale()
indicates that the language is changed and it's arabic, so is there are something missing with the configuration or any ideas what might cause that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Spring 配置中,当您设置验证器 bean 时,我认为您需要设置消息插值器:
其中插值器 bean 的类型为 LocaleContextMessageInterpolator (请参阅 此处了解更多信息信息)。 Hibernate Validator 不会自动知道查看 LocaleContextHolder。
您可能还需要设置validationMessageSource属性,但我认为它的默认值正确,因为您至少收到了英文错误消息。
In your Spring config when you are setting up your validator bean, I think you need to set the message interpolator:
where the interpolator bean would be of type LocaleContextMessageInterpolator (see here for more info). Hibernate Validator doesn't automatically know to look at LocaleContextHolder.
You might also need to set validationMessageSource property but I think its defaulted properly since you are at least getting the English error messages.