Hibernate验证器的国际化 [英] Internationalization in Hibernate validators

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

问题描述

Hibernate验证器是否支持国际化。我看到了jar,我可以看到各种ValidationMessages.properties文件。

Does Hibernate validators supports internationalization. I saw the jar and I could see the various ValidationMessages.properties file.

我们可以创建自己的自定义错误消息,这些消息将被国际化吗?我不想使用Hibernate验证器中默认提供的错误消息。

Can we create our own custom error messages which will be internationalized? I don't want to use error messages provided by default in Hibernate validators.

我们需要使用自己的自定义消息,它们应该国际化。

We need to use our own custom message and they should be internationalized.

以及Hibernate验证器支持的语言是什么。在jar中我看到了英语,法语,德语,土耳其语和蒙古语的属性文件。

As well what are the languages supported by the Hibernate validators. In jar I saw properties files for the English, French, German, Turkish and Mongolian.

我们可以添加更多语言,例如西班牙语,葡萄牙语等?

Can we add some more languages e.g. Spanish, Portuguese etc?

推荐答案

I18N是Bean Validation规范的组成部分。

I18N is integral part of the Bean Validation specification.

默认情况下,从名为ValidationMessages的资源包中检索消息。因此,只需为您需要的语言提供此捆绑包(例如ValidationMessages.properties),以覆盖来自Hibernate Validator的默认消息(从捆绑org.hibernate.validator.ValidationMessages中检索)。

By default messages are retrieved from a resource bundle named "ValidationMessages". So just provide this bundle (e.g. ValidationMessages.properties) for the language(s) you need to override the default messages from Hibernate Validator (which are retrieved from the bundle "org.hibernate.validator.ValidationMessages").

除了您提到的语言外,Hibernate Validator 4.2还将提供中文( HV- 359 )和西班牙语( HV-483 )消息。

Besides the languages you mentioned Hibernate Validator 4.2 will provide Chinese (HV-359) and Spanish (HV-483) messages.

如果您不想使用基于文件的资源包,您也可以提供自己的 MessageInterpolator ResourceBundleLocator 实现。

If you don't want to work with file based resource bundles at all you also can provide your own MessageInterpolator or ResourceBundleLocator implementations.

Usin g Hibernate Validator的 PlatformResourceBundleLocator 它也可以使用包含ValidationMessages以外的其他名称的包:

Using Hibernate Validator's PlatformResourceBundleLocator it's also possible to use bundles with other names than "ValidationMessages" like this:

Validation
    .byProvider(HibernateValidator.class)
    .configure()
    .messageInterpolator(
        new ResourceBundleMessageInterpolator(
            new PlatformResourceBundleLocator("com.mycompany.Messages")))
    .buildValidatorFactory()
    .getValidator();`

这篇关于Hibernate验证器的国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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