The Spring Documentation is very well for this theme: 17.8 Using locales You need two things:
- A
LocalResolver
- that is holds the local for an user and when the user send a request, it add the local to the request context (available viaRequestContext.getLocale()
) - often theSessionLocalResolver
orCookieLocaleResolver
is used for a normal web page based application - it is important that the bean id/name islocaleResolver
(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME="localeResolver"
) - (if the name is different, thenDispatcherServlet
will not find the locale resolver ! - A
LocaleChangeInterceptor
that change the locale when the user add?lang=de
to its request.
example configuration
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver"
name="localeResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang">
</bean>
...
</mvc:interceptors>
For your problem, it is likely that something with the LocalResolver
is wrong, maybe you just forget to set its id/name* to localeResolver
or you use a AcceptHeaderLocaleResolver
(that always "fall back" the the request header locale)
* see this answer for the difference between Id and Name