Talking about SpringMVC internationalization support, talking about springmvc Internationalization

Source: Internet
Author: User

Talking about SpringMVC internationalization support, talking about springmvc Internationalization

This week, the company's leadership hoped that I would work out an international solution for a project, study for two hours, and adopt SpringMVC's international support.

Principle: Register localeResolver (Regional parser) in DispatchServlet and add Locale Interceptor (LocaleChangeInterceptor) to detect changes in Request Parameters and Language Environments.

Register ResourceBundleMessageSource in the application context to define the path and name of the internationalized file in the program.

1. Language parser

In SpringMVC, the commonly used language parser includes

Header resolver: parses the accept-language in the Request Header Information Center of the client to obtain the required international language. For details, see = AcceptHeaderLocaleResolver.

Cookie resolver: parses the locale specified by the Cookie on the client to obtain the international information you need. For details, see = CookieLocaleResolver.

Session resolver: parses loacle information in the client request domain to obtain the required international information and store it in httpSession. For details, see = SessionLocaleResolver.

 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">    <property name="defaultLocale" value="en_US" /> </bean>

2. Regional interceptor

We need to register an interceptor for monitoring region change in DispatchServlet. It can help us detect Request Parameters and change the language environment according to the language corresponding to the request parameters.

 <mvc:interceptors>   <bean class="com.xxx.web.interceptor.IhgLocaleChangeInterceptor" /> </mvc:interceptors>

3. Configure international resources

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">   <property name="defaultEncoding" value="UTF-8" />   <property name="basename" value="classpath*:/ApplicationMessage" />   <property name="useCodeAsDefaultMessage" value="true" /> </bean>

4. Internationalization of pages

Here, I am using the fmt tag of jstl for internationalization. If you are interested, you can also use the message label of spring.

1) introduce the tag library <% @ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>

2) specify the name of the International File resource <fmt: setBundle basename = "ApplicationMessage"/>

3) read the file information through the key of the international resource file. <Fmt: message key = "security. account. number"/>

5. The SessionResolver parser is selected in step 2. Therefore, in the request, we need to splice locale = specific language identifier (for example, locale = zh_CN) after the url parameter ).

Note: splicing URLs on each page is troublesome. users usually want to do what they want. This language is preferred after selecting a language. If you are interested, you can consider extending LocaleChangeInterceptor to achieve better functions.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.