Configuring SessionLocaleResolver

There are two parts to configuring SessionLocaleResolver. The first one is to configure a localeResolver bean. The second one is to configure an interceptor to handle the change in the locale:

    <bean id="springMVCLocaleResolver" 
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>

<mvc:interceptors>
<bean id="springMVCLocaleChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>

The following definitions explain the workings of the preceding code block:

  • <property name="defaultLocale" value="en" />: By default, the en locale is used.
  • <mvc:interceptors>: LocaleChangeInterceptor is configured as a HandlerInterceptor. It would intercept all of the handler requests and check for the locale.
  • <property name="paramName" value="language" />: LocaleChangeInterceptor is configured to use a request parameter name called language to indicate the locale. So, any URL of the http://server/uri?language={locale} format would trigger a change in the locale.
  • If you append language=en to any URL, you would be using en locale for the duration of the session. If you append language=fr to any URL, then you would be using a French locale.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset