Internationalization in Hibernate validators

空扰寡人 提交于 2019-12-19 02:52:11

问题


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:


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


来源:https://stackoverflow.com/questions/6261858/internationalization-in-hibernate-validators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!