1
votes

I'm new to spring specially spring boot, i'm trying to use internationalisation, so far i'm using with normal controllers fine, but i can't make it work with Bean Validation using spring-data-rest.

I'm using the following code to start the application:

@EnableJpaRepositories
@SpringBootApplication
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Bean(name = "validator")
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

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

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

I can use locale fine with Controllers, but i can't make Bean Validation respect other locales, it always use default locale.

Any help?

I'm using spring-data-rest and spring-boot in version 1.2.5.RELEASE.

1
Is this for internationalizing entity fields or error messages? I am trying to internationalize spring-data-rest entity fields too. - aycanadal

1 Answers

0
votes

I found a solution, since no one helped i'm posting because can help someone else.

If i don't set the default location on SessionLocaleResolver it gets the locale from Accept-Header so i'll work.

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // Remove this line
    // slr.setDefaultLocale(Locale.US); 
    return slr;
}