Spring i18N

Spring i18N

Register Local Resolver

Spring will look for user register localResolver, if not found, spring will use configure AcceptHeaderLocaleResolver as default.

AcceptHeaderLocaleResolver

HTTP Request header contains information with key accept-language. This information will send by user browser.

accept-lang

 

SessionLocaleResolver

SessionLocaleResolver associate user’s request and retrieve locale and timezone.

CookieLocaleResolver

If your application does not use session, CookiesLocaleResolver store data in the user browser cookies. 3 properties you can define.

  • cookieName
  • cookieMaxAge
  • cookiePath

CookiesLocaleResolver

 

Example XML-Based Configuration

<!-- Step 1 define messageSource bean -->

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages"/>
</bean>


<!-- Step 2 define localeResolver bean -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />


<!-- Step 3 define interceptor for locale -->
<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i80n.LocaleChangeInterceptor">
       <property name="paramName" value="lang"/>
    </bean>
</mvc:interceptors>

 

Complete Code With Java-Based

https://github.com/loongest/springi18n

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.