Localization and internationalization of SPRINGMVC

Source: Internet
Author: User
Tags i18n locale

Most of the spring framework supports internationalization, just like SPRINGMVC. Dispatcherservlet enables you to dynamically configure in the local language of the client. This is done through the localeresolver.
When a request arrives, Dispatcherservlet will look for Localeresolver and will attempt to localize it if found. Using the Requestcontext.getlocale () method we can get the localized language of locale resolver interpretation.
Locale resolvers and interceptors are defined in the org.springframework.web.servlet.i18n package and can be configured in application context. Here is the configuration section of the localization parser.

First, Acceptheaderlocaleresolver
The locale resolvers is parsed based on the Accept-language request header information, which typically contains the local indication of the client's operational information.
Second, Cookielocaleresolver
This parser uses cookies to find client-specific language information. If found, this configuration is used. Using some of the properties of this locale resolvers, we can specify the name of the cookie or even the maximum time to live. Here is a configuration instance:
e.g.

[HTML]View Plaincopy
  1. <Bean id= "localeresolver" class=" Org.springframework.web.servlet.i18n.CookieLocaleResolver ">
  2. <property name= "cookiename" value="ClientLanguage"/>
  3. <!--in seconds. If set To-1, the cookie isn't persisted (deleted when browser shuts down) --
  4. <property name= "cookiemaxage" value="100000">
  5. </Bean>

The following is a list of cookielocaleresolver properties
Property Default Description
CookieName classname+local The name of the cookie
Cookiemaxage Integer.max_int Max Survival time
Cookiepath/Restrict the path to the program that can access the cookie.

Third, Sessionlocaleresolver
This parser allows you to parse the local settings through the session.
Iv. Localechangeinterceptor
By configuring Localechangeinterceptor, we can dynamically change the local language. It detects the parameters in the request and changes the region information. It calls Loacalresolver.setlocal () to configure. The following example shows that calling all *.view resources contains a variable named Sitelanguage to change the region information. So the following url:http://www.sf.net/home.view?sitelanguage=nl will change the site language to Dutch.
e.g.

[HTML]View Plaincopy
    1. <Bean id="Localechangeinterceptor"
    2. class="Org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    3. <property name= "paramname" value="sitelanguage"/>
    4. </Bean>
    5. <Bean id="Localeresolver"
    6. class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
    7. <Bean id="urlmapping"
    8. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    9. <property name="interceptors">
    10. <list>
    11. <ref bean="Localechangeinterceptor"/>
    12. </list>
    13. </Property>
    14. <property name="mappings">
    15. <value>/**/*. view=somecontroller</value>
    16. </Property>
    17. </Bean>

Localization and internationalization of SPRINGMVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.