본문 바로가기

개발

스프링 다국어 처리. spring Locale setting.

간단한 다국어 처리하는 순서.
1. Intercetping reuqest with a HandlerInterceptor.
    - 다국어 처리하기 전에 request를 Intercetping 하기위해 bean등록을 한다.
    (request에 있는 accept-language처리하기 위함.) 
    (bean 등록은 아래 xml에 나와있다.)

2. LocaleResolver
    - 받아온 request에서 LocaleResolver로 처리한다.
    - 처리 할 때 3가지의 방법이 있다. 개발자가 원하는 방법으로 처리한다.(쿠키,세션)

spring 에서 locales을 사용하는 방법.



스프링 아키텍쳐에서는 국제화를 지원해 줍니다. 
DispatcherServlet은 클라이언트의 로케일을 사용해서 자동적으로 메세지를 해결합니다. 이것은 LocaleResolver에 의해 가능하게 됩니다.

요청이 들어오면 DispatcherServlet은 locale resolver를 찾고 그리고 만약 찾는다면 그것을 이용해 locale을 세팅합니다. RequestContext.getLocale()을 이용해서 locale resolver에서 resolver된 locale을 찾아올 수 있습니다.

동으로 locale을 찾아 올려면 handler mapping에 인터셉터를 추가 시켜야합니다.


<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean id="id"
class="class" />
</mvc:interceptor>
</mvc:interceptors>

AcceptHeaderLocaleResolver

CookieLocaleResolver

SessionLocaleResolver


이 3가지 방법으로 Locale을 받아올수 있음. 

1. AcceptHeaderLocaleResolver

클라이언트가 보낸 request에 있는 header가 가지고 있는 accept-language를 검사한다. 대부분 헤더는 클라이언트의 os에 locale 정보를 가지고 있다.
이 AcceptHeaderLocaleResolver는 timezone에 대한 정보를 가지고 있지 않는다. 
그리고 setLocale()을 지원하지 않는다. (원하는대로 Locale을 설정할수 없다.)

2. CookieLocaleResolver
쿠키를 이용한 LocaleResolver.
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>

Table 17.4. CookieLocaleResolver properties

PropertyDefaultDescription

cookieName

classname + LOCALE

The name of the cookie

cookieMaxAge

Integer.MAX_INT

The maximum time a cookie will stay persistent on the client. If -1 is specified, the cookie will not be persisted; it will only be available until the client shuts down their browser.

cookiePath

/

Limits the visibility of the cookie to a certain part of your site. When cookiePath is specified, the cookie will only be visible to that path and the paths below it.



3. SessionLocaleResolver
세션을 이용한 LocaleResolver.

아래는 Locale과 Interceptor에 대한 스프링 레퍼런스 문서.


=================================================================

17.8 Using locales

Most parts of Spring’s architecture support internationalization, just as the Spring web MVC framework does. DispatcherServlet enables you to automatically resolve messages using the client’s locale. This is done with LocaleResolver objects.

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. Using theRequestContext.getLocale() method, you can always retrieve the locale that was resolved by the locale resolver.

In addition to automatic locale resolution, you can also attach an interceptor to the handler mapping (see Section 17.4.1, “Intercepting requests with a HandlerInterceptor” for more information on handler mapping interceptors) to change the locale under specific circumstances, for example, based on a parameter in the request.

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. Here is a selection of the locale resolvers included in Spring.

17.8.1 Obtaining Time Zone Information

In addition to obtaining the client’s locale, it is often useful to know their time zone. The LocaleContextResolver interface offers an extension to LocaleResolverthat allows resolvers to provide a richer LocaleContext, which may include time zone information.

When available, the user’s TimeZone can be obtained using the RequestContext.getTimeZone() method. Time zone information will automatically be used by Date/Time Converter and Formatter objects registered with Spring’s ConversionService.

17.8.2 AcceptHeaderLocaleResolver

This locale resolver inspects the accept-language header in the request that was sent by the client (e.g., 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.

17.8.3 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. Using the properties of this locale resolver, you can specify the name of the cookie as well as the maximum age. Find below an example of defining 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>

Table 17.4. CookieLocaleResolver properties

PropertyDefaultDescription

cookieName

classname + LOCALE

The name of the cookie

cookieMaxAge

Integer.MAX_INT

The maximum time a cookie will stay persistent on the client. If -1 is specified, the cookie will not be persisted; it will only be available until the client shuts down their browser.

cookiePath

/

Limits the visibility of the cookie to a certain part of your site. When cookiePath is specified, the cookie will only be visible to that path and the paths below it.


17.8.4 SessionLocaleResolver

The SessionLocaleResolver allows you to retrieve Locale and TimeZone from the session that might be associated with the user’s request.

17.8.5 LocaleChangeInterceptor

You can enable changing of locales by adding the LocaleChangeInterceptor to one of the handler mappings (see Section 17.4, “Handler mappings”). It will detect a parameter in the request and change the locale. It calls setLocale() on the LocaleResolver that also exists in the context. The following example shows that calls to all *.view resources containing a parameter named siteLanguage 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.

<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>

17.4.1 Intercepting requests with a HandlerInterceptor

Spring’s handler mapping mechanism includes handler interceptors, which are useful when you want to apply specific functionality to certain requests, for example, checking for a principal.

Interceptors located in the handler mapping must implement HandlerInterceptor from the org.springframework.web.servletpackage. 

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-localeresolver