Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Spring input validation language

I have a web application that exposes RESTful services with Spring Boot. I added validation on my models with the Hibernate implementation. Example :

public class PersonForm {

    @NotNull
    @Size(min=2, max=30)
    private String name;

    @NotNull
    @Min(18)
    private Integer age;
    ...
}

The problem is that the messages are rendered in english. Question : how can I force the application to render the messages in french ?

Regards.

like image 819
Liam Avatar asked Jul 10 '26 06:07

Liam


2 Answers

You need to add a spring bean in the context to set the default localization

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.FRENCH);
    return slr;
}
like image 139
Shibashis Avatar answered Jul 14 '26 14:07

Shibashis


In webflux,there is no SessionLocaleResolver, I simply add this:

    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH);
        SpringApplication.run(Application.class);
    }

you can also set -Duser.language=en

both works for me!

like image 27
copbint Avatar answered Jul 14 '26 14:07

copbint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!