Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Last active November 13, 2018 07:06
Show Gist options
  • Select an option

  • Save Ikhiloya/8ea8ed6d86e446b8f68d959fa106bf69 to your computer and use it in GitHub Desktop.

Select an option

Save Ikhiloya/8ea8ed6d86e446b8f68d959fa106bf69 to your computer and use it in GitHub Desktop.
Spring Boot Internalization Configuration Class
/**
* Configuration class for Internationalization
*/
@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {
/**
* * @return default Locale set by the user
*/
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
/**
* an interceptor bean that will switch to a new locale based on the value of the language parameter appended to a request:
*
* @param registry
* @language should be the name of the request param i.e localhost:8010/api/get-greeting?language=fr
* <p>
* Note: All requests to the backend needing Internationalization should have the "language" request param
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
registry.addInterceptor(localeChangeInterceptor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment