Internationalization in Hibernate validators

2019-02-13 19:21发布

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

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.

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?

1条回答
欢心
2楼-- · 2019-02-13 20:06

I18N is integral part of the Bean Validation specification.

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").

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

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

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();`
查看更多
登录 后发表回答