Spring MVC Internationalization Configuration

Source: Internet
Author: User
Tags i18n

Write in front

To consider supporting internationalization in the development of the project, the framework chooses the Spring MVC framework, where the relevant data are queried and how spring MVC is configured and internationalized.

Specific implementation

For the internationalization of spring MVC here I am based on the session, the following is done:

1. First we need to configure the following in the XML configuration file:

<bean id= "Messagesource" class= "Org.springframework.context.support.ResourceBundleMessageSource" >    < !--internationalization information is in the file name--                         <property name= "basename" value= "Lang.messages"/>       <!-- If you cannot find the corresponding code in the Internationalized resource file, use this code as the name---                   <property name= "Usecodeasdefaultmessage" value= "true"/>           </bean>

The core configuration here is to configure the resource file path "Lang.messages" because the project is built through MAVEN, so the language resource files can be placed directly under the resources path of the MAVEN project.

2, based on the session or the cookie to intercept the configuration, the following practices:

<mvc:interceptors>      <!--international operation interceptors if you are using (Request/session/cookie), you must configure--     <bean class= " Org.springframework.web.servlet.i18n.LocaleChangeInterceptor "/>  </mvc:interceptors>  <!-- Local parser--><bean id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver"/ >

3. Write controller code to switch languages.

@RequestMapping ("/lang") @ResponseBody public    String Lang (HttpServletRequest request) {    string langtype = Request.getparameter ("Langtype");            if (Langtype.equals ("zh")) {                locale locale = new Locale ("zh", "CN");                 Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale);             }            else if (langtype.equals ("en")) {                locale locale = new locale ("en", "US");                 Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale);            }            else                 request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name, Localecontextholder.getlocale ());            return null;    }

What is known is that the locale locale is set by the language type passed by the front end, and then spring MVC internally loads the. porperties file for the specified language according to the locale's dynamic selection, and the front end is called by a specific expression.

3, prepare language documents, here Examples of Chinese, English:messages_en.properties and messages_zh.properties

In the form of key-value pairs, the contents of the front-end interface (various languages) are stored, and the contents of the English-language files are simple to read.

Ms.sysname=manager System[email protected] Cqms.langen=englishms.langzh=chinese

4, front-end interface call.

The project uses the Velocity template engine build page, which is called in the. vm file in the following form:

#springMessage ("Ms.copyright")

Spring MVC Internationalization Configuration

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.