- Mastering Spring 5.0
- Ranga Rao Karanam
- 130字
- 2021-07-02 22:12:19
Configuring a SessionLocaleResolver
There are two parts to configuring a SessionLocaleResolver. The first one is to configure a localeResolver. The second one is to configure an interceptor to handle the change in 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>
Important things to note are as follows:
- <property name="defaultLocale" value="en" />: By default, en locale is used.
- <mvc:interceptors>: LocaleChangeInterceptor is configured as a HandlerInterceptor. It would intercept all the handler requests and check for the locale.
- <property name="paramName" value="language" />: LocaleChangeInterceptor is configured to use a request param 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.