I'm implementing internationalization and localization in my project where Locale is based in URL mapping of languages, for example, /en, /ja and /fr.
I spend the whole day browsing for answer but majority I read is using LocaleChangeInterceptor setter method setParamName("lang"). Basically changing Locale upon intercepting parameter lang:
http://localhost:8081/index?lang=fr
I want to make Locale change based in URL mapping like http://localhost:8081/index/en
Current configuration:
@Configuration
public class MessageConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.JAPAN);
slr.setLocaleAttributeName("session.current.locale");
slr.setTimeZoneAttributeName("session.current.timezone");
return slr;
}
@Bean
public LocaleChangeInterceptor localChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localChangeInterceptor());
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("language/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
Resources:
