Locale
Spring 架构的大多数部分都支持国际化,就像 Spring Web MVC 框架所做的那样。DispatcherServlet
允许使用客户端语言环境自动解析消息。这可以通过 LocaleResolver
对象实现。
Most parts of Spring’s architecture support internationalization, as the Spring web
MVC framework does. DispatcherServlet
lets you automatically resolve messages
by using the client’s locale. This is done with LocaleResolver
objects.
当一个请求到来时,DispatcherServlet
会寻找一个语言环境解析器,如果找到了一个,它会尝试使用它来设置语言环境。通过使用 RequestContext.getLocale()
方法,你始终可以检索由语言环境解析器解析的语言环境。
When a request comes in, the DispatcherServlet
looks for a locale resolver and, if it
finds one, it tries to use it to set the locale. By using the RequestContext.getLocale()
method, you can always retrieve the locale that was resolved by the locale resolver.
除了自动区域设置解析外,还可以将拦截器附加到处理程序映射(有关处理程序映射拦截器的更多信息,请参阅Interception)以在特定情况下更改区域设置(例如,基于请求中的某个参数)。
In addition to automatic locale resolution, you can also attach an interceptor to the handler mapping (see Interception for more information on handler mapping interceptors) to change the locale under specific circumstances (for example, based on a parameter in the request).
语言环境解析器和拦截器在 org.springframework.web.servlet.i18n
包中定义,以一般的方式在你的应用程序上下文中进行配置。Spring 中包含了以下范围的语言环境解析器。
Locale resolvers and interceptors are defined in the
org.springframework.web.servlet.i18n
package and are configured in your application
context in the normal way. The following selection of locale resolvers is included in
Spring.
Time Zone
除了获取客户端的区域设置外,通常了解其时区也很有用。LocaleContextResolver
接口提供对 LocaleResolver
的扩展,它使得解析器能够提供一个更丰富的 LocaleContext
,其中可能包括时区信息。
In addition to obtaining the client’s locale, it is often useful to know its time zone.
The LocaleContextResolver
interface offers an extension to LocaleResolver
that lets
resolvers provide a richer LocaleContext
, which may include time zone information.
如果有,可以使用 RequestContext.getTimeZone()
方法获取用户的 TimeZone
。时区信息由向 Spring 的 ConversionService
注册的任何 Date/Time Converter
和 Formatter
对象自动使用。
When available, the user’s TimeZone
can be obtained by using the
RequestContext.getTimeZone()
method. Time zone information is automatically used
by any Date/Time Converter
and Formatter
objects that are registered with Spring’s
ConversionService
.
Header Resolver
此语言环境解析器检查客户端发送的请求中的 accept-language
标头(例如,Web 浏览器)。通常,此标头字段包含客户端操作系统的语言环境。请注意,此解析器不支持时区信息。
This locale resolver inspects the accept-language
header in the request that was sent
by the client (for example, a web browser). Usually, this header field contains the locale of
the client’s operating system. Note that this resolver does not support time zone
information.
Cookie Resolver
此语言环境解析器检查客户端上可能存在的 Cookie
,以查看是否指定了 Locale
或 TimeZone
。如果是,它将使用指定的信息。通过使用此语言环境解析器的属性,你可以指定 cookie 的名称以及 maximum age。以下示例定义了一个 CookieLocaleResolver
:
This locale resolver inspects a Cookie
that might exist on the client to see if a
Locale
or TimeZone
is specified. If so, it uses the specified details. By using the
properties of this locale resolver, you can specify the name of the cookie as well as the
maximum age. The following example defines a CookieLocaleResolver
:
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="clientlanguage"/>
<!-- in seconds. If set to -1, the cookie is not persisted (deleted when browser shuts down) -->
<property name="cookieMaxAge" value="100000"/>
</bean>
下表描述了属性 CookieLocaleResolver
:
The following table describes the properties CookieLocaleResolver
:
Property | Default | Description |
---|---|---|
|
class name + LOCALE |
The name of the cookie |
|
Servlet container default |
The maximum time a cookie persists on the client. If |
|
/ |
Limits the visibility of the cookie to a certain part of your site. When |
Session Resolver
SessionLocaleResolver
允许你从可能与用户的请求关联的会话中检索 Locale
和 TimeZone
。与 CookieLocaleResolver
相反,此策略将本地选择的语言环境设置存储在 Servlet 容器的 HttpSession
中。因此,这些设置对于每个会话都是临时的,并且因此在每个会话结束时都会丢失。
The SessionLocaleResolver
lets you retrieve Locale
and TimeZone
from the
session that might be associated with the user’s request. In contrast to
CookieLocaleResolver
, this strategy stores locally chosen locale settings in the
Servlet container’s HttpSession
. As a consequence, those settings are temporary
for each session and are, therefore, lost when each session ends.
请注意,与外部会话管理机制(例如 Spring Session 项目)没有直接关系。此 SessionLocaleResolver
根据当前的 HttpServletRequest
评估和修改相应的 HttpSession
属性。
Note that there is no direct relationship with external session management mechanisms,
such as the Spring Session project. This SessionLocaleResolver
evaluates and
modifies the corresponding HttpSession
attributes against the current HttpServletRequest
.
Locale Interceptor
你可以通过将 LocaleChangeInterceptor
添加到其中一个 HandlerMapping
定义来启用语言环境的更改。它检测请求中的参数并相应地更改语言环境,从而在分发器的应用程序上下文中对 LocaleResolver
调用 setLocale
方法。以下示例表明,所有包含名为 siteLanguage
的参数的 *.view
资源的调用现在都会更改语言环境。所以,例如,对 URL https://www.sf.net/home.view?siteLanguage=nl
的请求会将网站语言更改为荷兰语。以下示例显示了如何拦截语言环境:
You can enable changing of locales by adding the LocaleChangeInterceptor
to one of the
HandlerMapping
definitions. It detects a parameter in the request and changes the locale
accordingly, calling the setLocale
method on the LocaleResolver
in the dispatcher’s
application context. The next example shows that calls to all *.view
resources
that contain a parameter named siteLanguage
now changes the locale. So, for example,
a request for the URL, https://www.sf.net/home.view?siteLanguage=nl
, changes the site
language to Dutch. The following example shows how to intercept the locale:
<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>