Internationalization in Hibernate Validators

Does the Hibernate validator support internationalization? I saw a jar and I could see the ValidationMessages.properties file.

Can we create custom error messages that will be internationalized? I do not want to use the error messages provided by default in Hibernate validators.

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

In addition, which languages ​​are supported by Hibernate validators. In the bank, I saw property files for English, French, German, Turkish, and Mongolian.

Is it possible to add a few more languages, for example. Spanish, Portuguese, etc.

+6
source share
1 answer

I18N is an integral part of the Bean validation specification.

By default, messages are retrieved from a resource set named "ValidationMessages". Therefore, simply specify this package (for example, ValidationMessages.properties) for the language (s) that you need to override the default messages from Hibernate Validator (which are extracted from the package "org.hibernate.validator.ValidationMessages").

In addition to the languages ​​you mentioned, Hibernate Validator 4.2 will provide Chinese ( HV-359 ) and Spanish ( HV-483 ).

If you do not want to work with file-based file packages at all, you can also provide your own MessageInterpolator or ResourceBundleLocator .

Using the Hibernate Validator PlatformResourceBundleLocator , it is also possible to use packages with names other than "ValidationMessages", like this:

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

Source: https://habr.com/ru/post/889926/


All Articles