SPRINGMVC resource internationalization Implementation and FAQs

Source: Internet
Author: User
Tags i18n naming convention

The internationalization of resources can easily realize the switch of Web project language, which solves the problem that Web projects display different language interfaces on demand.

SPRINGMVC's resource internationalization is based on the Java.util.ResourceBundle implementation of the JDK, and the SPRINGMVC package is simple to implement:

The simple implementation of the following steps:

1. Configure Resourcebundlemessagesource in the SPRINGMVC configuration file (The role of this class is to bind the resource file, depending on the locale value, display different languages)

1     <!--resource Internationalization related configuration -2     <BeanID= "Messagesource"class= "Org.springframework.context.support.ResourceBundleMessageSource">3         <!--if the corresponding code is not found in the Internationalized resource file, use this code as the name -4         < Propertyname= "Usecodeasdefaultmessage"value= "true" />5         <!--the default encoding format is Utf-8 -6         < Propertyname= "Defaultencoding"value= "UTF-8" />7         <!--the file name where internationalization information resides - 8         < Propertyname= "Basenames">                    9             <List>Ten                 <value>I18n.messages</value> One             </List> A         </ Property> -     </Bean>

(PS:basenames Attribute, value is the address of the resource file)

2. Configure Localeresolver (This class indicates where the locale should be stored)

Localeresolver has three options, based on request, session-based, cookie-based, because the request-based implementation does not support manual switching, and it is recommended to use session-based implementations

Three ways to configure the following:

A. Request-based implementation:

    <!---    < id = "Localeresolver" class  = "Org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>

B. Session-based implementations:

 <!--  Resource internationalization based on session  -->  <  bean  id  = "Localeresolver"   class  Span style= "color: #0000ff;"        >= "Org.springframework.web.servlet.i18n.SessionLocaleResolver"  >  <  property  name  = "Defaultlocale"   = "en"     />  </ bean  >  

   c. cookie-based implementation

 <!--  Resource internationalization based on cookies  -->  <  bean  id  = "Localeresolver"   class  Span style= "color: #0000ff;" >= "Org.springframework.web.servlet.i18n.CookieLocaleResolver"  >  Span style= "color: #0000ff;" ><  property  name  = "Defaultlocale"   value  =" en " Span style= "color: #0000ff;" >/>  </ bean  >  

  3. Configure the Localechangeinterceptor interceptor (the role of this class is to eradicate different localeresolve to set locale in different ways)

    <mvc:interceptors>        <BeanID= "Localechangeinterceptor"class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor">            < Propertyname= "ParamName"value= "Langtype" />        </Bean>    </mvc:interceptors>

  (ParamName attribute, which is the parameter name of the switch language in the request)

4. Writing Resource Files

The naming convention for resource files is basename [_language] [_country] [_variant].properties

    Languange Country variant needs to be a value in the locale class

5. Page gets the value in the resource file (for example, JSP)

<%@ taglib prefix= "Spring" uri= "http://www.springframework.org/tags"%><spring:message code= " Comment.management "/> <!--code value is the corresponding key--in the resource file

6. Page switching language

In this way, the internationalization of the resources, will intercept all requests, as long as the request with the parameters defined in the Localechangeinterceptor, the interceptor will modify the stored locale, the implementation of language switching

Frequently asked questions (highlights) (to be added, please later):

1. Do not use interceptors based on session

2. Load order of resource files

3. Settings for the default language

    

    

SPRINGMVC resource internationalization Implementation and FAQs

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.