Switching locale on the login page


Spring provides some proprietary ways to change the display locale. This documentation will describe how to change it via an URL query string parameter and how to persist it with a cookie. This method may or may not work in any particular Spring version without changes.

The configurations below are intended to be placed in conf/mvc-beans.xml, whch you would need to create (you can copy one of the other native Spring beans files as a template to start from).

General Configuration

Enabling locale change persistance via a cookie is done by defining the CookieLocaleResolver bean and optionally changing it's defaults:

CookieLocaleResolver configuration
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieName" value="lang"/> </bean>

Here we changed the cookiename to be lang instead. Now you can set a cookie called lang to be any locale your IDP supports, e.g. en_GB . This can be done via Javascript e.g. or use the LocaleChangeInterceptor below.

URL query string based locale changing

To change the locale via an URL query string parameter you have to define two beans: a LocaleChangeInterceptor that will inspect request parameters and a LocaleResolver that will remember the locale change, like the CookieLocaleResolver above or SessionLocaleResolver (the present-by-default AcceptHeaderLocaleResolver does not allow locale changes).

LocaleChangeInterceptor configuration
<mvc:interceptors> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"/> </bean> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="en"/> </bean>

Here we enabled changing the locale based on the URL query string parameter lang and set a fallback locale of "en".


See Spring documentation for: