import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
@Configuration
public class Config {
@Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver ret = new CookieLocaleResolver();
ret.setDefaultLocale(new Locale("en_US"));
return ret;
}
@Bean
public MessageSource messageSource() {
final ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasename("classpath:lang");
ret.setDefaultEncoding("UTF-8");
return ret;
}
@Bean
public HandlerMapping handlerMapping() {
final LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("language");
final DefaultAnnotationHandlerMapping ret = new DefaultAnnotationHandlerMapping();
ret.setInterceptors(new Object[] { interceptor });
return ret;
}
}
The above is my annotation configuration. I've basically translated this tutorial's XML.
Strangely it doesn't work when I go to ...?language=fr
.
However, the following does work (in app-servlet.xml
) (notice here it's using locale
):
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>
</mvc:interceptors>
Another important thing to note is that when I put breakpoints on the above methods, all of the three of them, every breakpoint does break, which implies that "someone" is reading the values.
So, why wouldn't my annotation based interceptor doesn't work?
Extending config class by WebMvcConfigurerAdapter
may help.
to add interceptor entry override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
method.
also add bean entry for LocaleChangeInterceptor
@Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
In addition to what swap says, you need to add:
@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver(){
return new CookieLocaleResolver();
}
The bean name is important. That's the way spring will resolve the correct locale resolver.
Alternatively you can return the SessionLocaleResolver.
If you do not add this you will get the following error:
Cannot change HTTP accept header - use a different locale resolution strategy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With