Skip to content
Advertisement

Hibernate Validator: Violation Message Language

I have a test class where I am testing a domain model which is annotated with e.g. @NotNull

In my test class I am first getting the validator

JavaScript

Later I have a JUnit Test where I am testing the domain model (lets say a Person)

JavaScript

Lets say, I want to retrieve the first violation Message I do:

JavaScript

I haven’t set any custom violation message on the @NotNull annotation. So hibernate validator will pull the default message from the Resource Bundle which is in the hibernate-validator-jar. My path looks like this:

JavaScript

In this Resource Bundle there are quite a few languages supported (English, German, French, …)

Example

@NotNull violation message in German

JavaScript

@NotNull violation message in English

JavaScript

Question:

When I am testing, how can I enforce Hibernate Validator to choose a specific language for the violation message from the Resource Bundle? Right now, I am getting English violation messages. However, on another machine German.

Advertisement

Answer

Summary condensed from the reference docs:

By default, the JVM’s default locale (Locale#getDefault()) will be used when looking up messages in the bundle.

Without touching source code or fiddle with your OS/User seetings, try this for spanish messages:

JavaScript

Hibernate Validator 5.1 Reference

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement