休眠验证和本地化错误消息? [英] Hibernate Validation and localized error messages?

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

问题描述

休眠,我想为休眠注释提供本地化的错误消息 所以我创建了属性文件ValidatorMessages.properties,ValidatorMessages_ar.properties

hibernate and i want to provide localized error messages for hibernate annotations so i created to properties files ValidatorMessages.properties, ValidatorMessages_ar.properties

并将它们放在资源文件夹中,我正在使用messageSource来读取属性文件:

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()表示语言已更改并且是阿拉伯语,因此配置中是否缺少某些内容或任何想法可能导致该错误?

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 ?

推荐答案

在您的Spring配置中,设置验证器bean时,我认为您需要设置消息插值器:

In your Spring config when you are setting up your validator bean, I think you need to set the message interpolator:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="messageInterpolator" ref="interpolator" />
</bean>

插值器bean的类型为LocaleContextMessageInterpolator(请参见

where the interpolator bean would be of type LocaleContextMessageInterpolator (see here for more info). Hibernate Validator doesn't automatically know to look at LocaleContextHolder.

您可能还需要设置validationMessageSource属性,但我认为它已正确设置为默认值,因为您至少会收到英语错误消息.

You might also need to set validationMessageSource property but I think its defaulted properly since you are at least getting the English error messages.

这篇关于休眠验证和本地化错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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