18.8.5LocaleChangeInterceptor
You can enable changing of locales by adding theLocaleChangeInterceptor
to one of the handler mappings (seeSection18.4, “Handler mappings”). It will detect a parameter in the request and change the locale. It callssetLocale()
on theLocaleResolver
that also exists in the context. The following example shows that calls to all*.view
resources containing a parameter namedsiteLanguage
will now change the locale. So, for example, a request for the following URL,http://www.sf.net/home.view?siteLanguage=nl
will change the site language to Dutch.
您可以通过将LocaleChangeInterceptor
添加到其中一个处理程序映射(请参见第18.4节“处理程序映射”)来启用区域设置的更改。 它将检测请求中的参数并更改语言环境。 它调用上下文中也存在的LocaleResolver
上的setLocale()
。 以下示例显示调用包含名为siteLanguage
的参数的所有* .view
资源现在将更改语言环境。 因此,例如,对以下URL(http://www.sf.net/home.view?siteLanguage=nl
)的请求会将网站语言更改为荷兰语。
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>