Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Overview

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.

...

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

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

...

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
Code Block
languagexmltitle 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>

...

See Spring documentation for: