I'm trying to implement internationalization (English, French) in my Spring Boot application. I've succeeded in being able to read the locale from the URL and have the texts in the page appear in the intended language. For example, if I type ?lang=fr
then all texts appear in French.
However, my application consists of several pages which I navigate around using the <a href=...>
tag. Strangely, when I click on a link to go to another page, the language sometimes remains the same, but sometimes changes back to the default English language. What's stranger is that this behavior is not consistent; sometimes it takes 3 clicks for the language to revert to English, and sometimes with just 2 clicks, and the behavior differs from browser to browser too (I use Chrome and Firefox).
My understanding is that the locale is stored as a session cookie, intercepted by the LocaleChangeInterceptor
bean (which is added in the InterceptorRegistry
) and resolved by the LocaleResolver
bean. If this is true, then I don't understand why the locale would (sometimes) change from page to page.
Could the problem be in that I'm using the <a href=...>
tag for navigating around the pages? Would using a button and jQuery be a better option?
Any help in this matter is greatly appreciated.
Edit
Here's my Locale configuration class:
@Configuration
public class LocaleConfig implements WebMvcConfigurer{
@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());
}
}