开发者

Internationalization in Hibernate validators

Does Hibernate validators supports internationa开发者_JS百科lization. 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜