I am using following class to manage localization in my spring boot application.
@Configuration
public class MessageConfig implements WebMvcConfigurer {
private Logger logger = LoggerFactory.getLogger(MessageConfig.class);
/**
* Message source for localization.
* @return message source.
*/
@Bean
public MessageSource messageSource() {
if (logger.isDebugEnabled()) {
logger.debug("Creating a ResourceBundleMessageSource.");
}
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
source.setUseCodeAsDefaultMessage(true);
return source;
}
/**
* Utility for localization.
* @return locale resolver.
*/
@Bean
public LocaleResolver localeResolver() {
if (logger.isDebugEnabled()) {
logger.debug("Creating a locale resolver.");
}
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
/**
* Utility for localization.
* @return locale change interpreter.
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
if (logger.isDebugEnabled()) {
logger.debug("Creating a locale change interceptor.");
}
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
if (logger.isDebugEnabled()) {
logger.debug("Adding locale change interceptor to registry.");
}
registry.addInterceptor(localeChangeInterceptor());
}
}
My application decides the locale to be used based on the lang URL parameter sent to the API.
When I send localhost:8080/api
, it sends content in default language, which is english.
If I send localhost:8080/api?lang=si
, it sends content in Sinhala language.
However, if I send a request to localhost:8080/api
without specifying the lang, I receive content in Sinhala language, which was used in the previous call, instead of the default locale.
I am using LocaleContextHolder
in service layer to decide the content of the output.
My understanding is each seperate call to API will be handled by a new thread. So, why the default locale has been changed to 'si', in this API call even when I have not specified the lang?
The LocaleChangeInterceptor
changes the language based on the fact if the request parameter is present or not. If this parameter is present it will get the Locale
and use the LocaleResolver.setLocale
method to change the language for the current user.
All the code that needs the Locale
for translating messages use the LocaleResolver.resolveLocale
method to obtain it.
Now as you are using the SessionLocaleResolver
it will be stored in the users HttpSession
. So after it being changed it will remain the same for the current http session NOT for other session. If you remove the session cookie in your browser it will be the default language again.
You can test this by opening a new differnt browser (not the same browser as this will copy the session state) or an incognito window of your browser and check your site. It will still be in the default language and not the changed language.