Vaadin & Spring Boot - Alternative translations within ValidationMessages.properties are not picked up

366 Views Asked by At

I tried to use BeanFieldGroup<Entity> in Vaadin Spring Boot, with javax.validator and Hibernate validators.

@NotBlank(message = "{may.not.null}")
@Column(name = "name", unique = true)
private String name;

and I created two files: ValidationMessages_en.properties:

may.not.null=not null

and ValidationMessages_fr.properties:

may.not.null=non null

But even when I change the language to French, the validator message is still from ValidationMessages_en.properties.

Have any of you any idea about this, please?

1

There are 1 best solutions below

0
On

The BeanFieldGroup creates the BeanValidator with the locale, which is set to the component under validation. Your question does not mention how you create the text field for the name property, but let's assume that you create it this way:

TextField nameTextField = new TextField("Name");

The important factor here is to make sure that the nameTextField has correct locale before you bind the properties with BeanFieldGroup (you can check the locale with nameTextField.getLocale()).

So, try to configure your TextField to use the session locale like this:

nameTextField.setLocale(UI.getCurrent().getLocale());

This piece of code takes the locale from the request ("Accept-Language" request header) and sets it to the field -> the BeanFieldGroup will then create BeanValidators with the correct locale and the validation messages are localized (this happens in BeanFieldGroup#configureField).

However use this code only to check that the problem is really only in that your text field does not have correct Locale. It really does not mean that you have to set the Locale for each and every field out there... That's because in Vaadin (at least in Vaadin 7) the session (request based) Locale should be inherited by component from the parent. So if you add TextField to the Layout, then the TextField inherits the locale of the Layout, the Layout inherits it from the View and so on (all the way up to UI.getCurrent().getLocale() - so check that you have desired locale there). Since the inheritance happens during addComponent() call, it's my guess, that, maybe, in your case, it would be enough to call the addComponent() method (where you are adding the TextField to the layout) before you actually use the BeanFieldGroup binding.

More info about your problem here: Remember to set the Locale

Note: I tried to code your issue right now, I faced the same problem as you had and resolved it by moving the addComponent() call before the BeanFieldGroup.