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"的资源包中检索的.因此,只需为您需要覆盖来自 Hibernate Validator 的默认消息(从包org.hibernate.validator.ValidationMessages"中检索)的语言提供这个包(例如 ValidationMessages.properties).

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.

使用 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();`

相关文章