Hibernate 验证和本地化错误消息?

发布于 2024-10-31 02:12:47 字数 1657 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-11-07 02:12:47

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

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="messageInterpolator" ref="interpolator" />
</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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文