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?
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 thename
property, but let's assume that you create it this way:The important factor here is to make sure that the
nameTextField
has correct locale before you bind the properties withBeanFieldGroup
(you can check the locale withnameTextField.getLocale()
).So, try to configure your TextField to use the session locale like this:
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 inBeanFieldGroup#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 duringaddComponent()
call, it's my guess, that, maybe, in your case, it would be enough to call theaddComponent()
method (where you are adding the TextField to the layout) before you actually use theBeanFieldGroup
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.