Internationalization of the SPRINGMVC Learning Series (8)

Source: Internet
Author: User
Tags getmessage i18n locale

I. Internationalization implementations based on browser requests:

1) Add in spring's configuration file

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

2) Create a folder locale under Resources, add Messages.properties, Messages_zh_cn.properties, messages_en_us.properties three files under locale

Like the content under Resources/locale/messages_zh_cn.properties.

Message.login.fail= Login failed message.login.success= Login succeeded

Like the content under Resources/locale/messages_en_us.properties.

Message.login.fail=login Failmessage.login.success=login Success

3) Read the contents of the resource file in the background and return to the foreground

Add a method to the controller file

    @RequestMapping (value = "/testmessage.do") public    String testMessage (httpservletrequest request,model Model)    {        logger.debug ("Enter Maincontroller.testmessage ()");                Get internationalized information from background code        RequestContext requestcontext = new RequestContext (request);        Model.addattribute ("Message", Requestcontext.getmessage ("message.login.success"));        return "Testmessage";    }

4) Front desk display

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD "><% @taglib Prefix= "Spring" uri= "http://www.springframework.org/tags"%>
Display the contents of internationalized files directly in the foreground
<spring:message code= "Message.login.fail" ></spring:message></body>

  

Two. Session-based internationalization implementation:

1) Add in spring's configuration file

<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>  <bean Id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

2) in the background

@RequestMapping (value = "/sessiontest", method = {Requestmethod.get})
Public String seesiontest (HttpServletRequest request, model model,
@RequestParam (value = "Langtype", defaultvalue = "zh") String 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 ());

Get internationalized information from background code
RequestContext RequestContext = new RequestContext (request);
Model.addattribute ("Message", Requestcontext.getmessage ("message.login.success"));
return "Testmessage";
}

3) Ibid.

4) on the JSP page, add

Use session Toggle <a href= "${ctx}/sessiontest?langtype=zh" > Chinese </a> | <a href= "${ctx}/sessiontest?langtype=en" > English </a><br/>
${message}<br>

Three. The internationalization of cookies-based implementations:

Similar to session, does not exist with the session mode

1) Add in spring's configuration file

<bean id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

2) Backstage

 @RequestMapping (value = "/CookieTest", method = {Requestmethod.get}) public String CookieTest (httpservletrequest request, httpservletresponse response, model, @RequestParam (value = "lan GType ", defaultvalue =" zh ") String Langtype) {if (Langtype.equals (" zh ")) {locale locale = n            EW Locale ("zh", "CN");        (New Cookielocaleresolver ()). SetLocale (Request, response, locale);            } else if (Langtype.equals ("en")) {locale locale = new locale ("en", "US");        (New Cookielocaleresolver ()). SetLocale (Request, response, locale);                } else (new Cookielocaleresolver ()). SetLocale (Request, Response, Localecontextholder.getlocale ());        Get internationalized information from background code requestcontext RequestContext = new RequestContext (request);        Model.addattribute ("Message", Requestcontext.getmessage ("message.login.success"));    return "Testmessage"; }

 

3) Ibid.

4) on the JSP page, add

Use cookies to switch <a href= "${ctx}/cookietest?langtype=zh" > Chinese </a> | <a href= "${ctx}/cookietest?langtype=en" > English </a><br/>
${message}<br>

Four. Implementation of internationalization based on URL request:

1) Customizing a class

Import Java.util.locale;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.dispatcherservlet;import Org.springframework.web.servlet.localeresolver;public class Myacceptheaderlocaleresolver extends Acceptheaderlocaleresolver {    private Locale mylocal;    Public Locale Resolvelocale (HttpServletRequest request) {        return mylocal;    }     public void SetLocale (HttpServletRequest request, httpservletresponse response, locale locale) {        mylocal = locale;< c6/>}  }

2) Add in spring's configuration file

<bean id= "Localeresolver" class= "Xx.xxx.xxx.MyAcceptHeaderLocaleResolver"/>

"Xx.xxx.xxx" is the package name of the Myacceptheaderlocaleresolver class you just added.

After saving, you can attach LOCALE=ZH_CN or locale=en_us after the requested URL.

 

Reference: http://www.cnblogs.com/liukemng/p/3750117.html

Internationalization of the SPRINGMVC Learning Series (8)

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.