Locale Changing and Resolution


Using Spring locale resolution techniques, you can easily localize your web application. Locale resolution can be done in a variety of ways, based on all kinds of criteria, ranging from the accept header to a cookie placed at the client's machine.

The main component involved with localization is the org.springframework.web.servlet.LocaleResolver. It has two methods: one to resolve the locale based on the incoming request and one to set the locale, using the HttpServletResponse, for example, thus making it persistent (using a cookie for CookieLocaleResolver, for example). The latter is optional because not all places in which locale information is stored are also writable (think of headers in an HttpServletRequest).

 Locale resolveLocale(HttpServletRequest req);     void setLocale(HttpServletRequest req, HttpServletResponse res, Locale loc); 

Using a LocaleResolver is simple; just define one in your application context and it will automatically be picked up by the MVC framework. Theorg.springframework.web.servlet.support.RequestContextUtils has support for retrieving the locale using

Locale RequestContextUtils.getLocale(HttpServletRequest request);

and

LocaleResolver RequestContextUtils.getLocaleResolver(HttpServletRequest request);

Spring provides three LocaleResolvers and an additional LocaleChangeInterceptor capable of changing the locale based on HTTP request parameters associated with an incoming request. The three are as follows, and we'll discuss each in turn:

  • AcceptHeaderLocaleResolver

  • SessionLocaleResolver

  • CookieLocaleResolver

AcceptHeaderLocaleResolver uses the accept-header field in the header sent by the client’s browser to retrieve the locale. Retrieving the locale from the request simply corresponds to a call to HttpServletRequest.getLocale(). The AcceptHeaderLocaleResolver does not support setting the locale: It is intended to identify a specified locale.

SessionLocaleResolver inspects the HTTP session (if any) for a Locale object. If no session exists, or the Locale yesobject could not be found in the session, the accept header is used as a fallback. This LocaleResolver does support setting of the locale.

The third locale resolver Spring provides is the CookieLocaleResolver and we'll cover this one in more detail, together with the LocaleChangeInterceptor, which will allow for changing of the locale based on HTTP parameters sent along with the incoming request. First, let's get the CookieLocaleResolver up and running:

<bean >   <property name="cookieMaxAge"><value>3600</value></property> </bean>

If you need to, you can also tweak the cookie path, limiting the visibility of the cookie and the name of the cookie.

Next, using the LocaleChangeInterceptor, we will allow the user to (using links including parameters) modify the locale.

<bean >   <property name="interceptors">     <list>       <bean >         <property name="paramName"><value>language</value></property>       </bean>     </list>   </property>   .... </bean>

Using this handler mapping (of course along with some additional mapping of URLs to controllers), users will be able to change the locale when executing requests. In the following example, the locales will be changed to Dutch and English, respectively.

 http://www.springframework.org/sample/editAccount.html?language=nl http://www.springframework.org/sample/index.view?language=en 



Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net