Hi everyone I got Spring boot application and now I am trying to implement i18n. I already created a Configuration class which implements WebMvcConfigurer and create beans for LocalResolver, MessageSource, and Interceptors. and it works perfectly when I am changing my language from the browser it changes language for all views of the application. but now the problem is that I wrote some language selector to change language and it changes language only for one view. how can I implement this to assign selected language for all views of the application? thank you.
this is my configuration
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver() {
CookieLocaleResolver resolver= new CookieLocaleResolver();
resolver.setCookieDomain("myAppLocaleCookie");
// 60 minutes
resolver.setCookieMaxAge(60*60);
return resolver;
}
@Bean(name = "messageSource")
public MessageSource getMessageResource() {
ReloadableResourceBundleMessageSource messageResource= new ReloadableResourceBundleMessageSource();
// Read i18n/messages_xxx.properties file.
// For example: i18n/messages_en.properties
messageResource.setBasename("classpath:i18n/messages");
messageResource.setDefaultEncoding("UTF-8");
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
}
}
this is my home controller mapping
@GetMapping("/international")
public String getInternationalPage() {
return "home";
}
and peace of my thymeleaf view
<select id="locales">
<option value="choose language"></option>
<option value="en" th:text="english"></option>
<option value="hy" th:text="armenian"></option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#locales").change(function () {
var selectedOption = $('#locales').val();
if (selectedOption != ''){
window.location.replace('international?lang=' + selectedOption);
}
});
});
</script>