The SPRINGMVC internationalization based on session

Source: Internet
Author: User
Tags getmessage i18n

The project uses SPRINGMVC framework, need to dynamically switch languages, find some information, finally decided to use session-based dynamic switching, to achieve dynamic switching Chinese, English, Korean, in fact, the Chinese translation into other languages display

SPRINGMVC internationalization includes two aspects, one is the internationalization of the front page, Spring has its own label can be implemented, very convenient, and the other is the background Java code involves the internationalization of Chinese.

1, the overall structure of the project

The main configuration for the bean is configured in Spring-mvc.xml, and the Messages folder contains the content format that needs to be translated:the format of key = value

English:

Chinese:

Korean:

Note that in the configuration file, the Chinese need to be translated into Unicode encoding, or the translation will appear garbled.

2, add the following configuration in SPRING-MVC:
1<!--International resource configuration, resource file Binder--2<bean id= "Messagesource"class= "Org.springframework.context.support.ReloadableResourceBundleMessageSource" >3<!--International Resource file configuration, specify properties file Location--4<property name= "basename" value= "Classpath:messages/message"/>5<!--if the corresponding code is not found in the Internationalized resource file, use this code as the name--6<property name= "Usecodeasdefaultmessage" value= "true"/>7</bean>8<!--dynamic switching internationalization, internationalization in session--9<bean id= "Localeresolver"class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver" ></bean>Ten<mvc:interceptors> One<!--international Operation Interceptor If it is based on (Request/session/cookie), it must be configured-- A<beanclass= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor" > -<!--Use this parameter to decide to get that profile-- -<property name= "paramname" value= "language"/> the</bean> -</mvc:interceptors>

It is important to note that the value of basename needs to be configured to the level Messages/message . This is where the internationalized file is stored, until the first underline.

3, the front page implementation

1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2pageencoding= "UTF-8"%>3<% @taglib prefix= "Spring" uri= "Http://www.springframework.org/tags"%>4<% @taglib prefix= "MVC" uri= "Http://www.springframework.org/tags/form"%>5<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >678<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">9<title>insert title here</title>Ten One<body> ASelect language: <a href= "? language=zh_cn" > Chinese </a> | <a href= "? Language=en_us" > English </a> | <a href= "? Language=ko_kr" > Korean </a> -<br></br> - This shows the translation of "Hello" after selecting the corresponding language (front label translation): the<spring:message code= "Hello"/> -<br></br> - This shows the translation of "welcome you" after selecting the corresponding language (background code translation): ${welcome} -  +</body> -

Note that the page introduces the spring tag, code I write in Chinese, corresponding to the three translation files in the key if code in the translation file does not find the corresponding key is used in the code content this configuration in spring '-MVC specified.

4, the implementation of the background code
1  PackageCom.ccg.controller;2 3 4 5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletResponse;7 8 ImportOrg.springframework.stereotype.Controller;9 Importorg.springframework.web.bind.annotation.RequestMapping;Ten ImportOrg.springframework.web.servlet.ModelAndView; One ImportOrg.springframework.web.servlet.support.RequestContext; A  -  - @Controller the@RequestMapping ("Test") -  Public classTestController { -      -@RequestMapping ("View") +      PublicModelandview View (httpservletrequest request, httpservletresponse response) { -         //Spring translation using the Req.getmessage () method +RequestContext req =NewRequestContext (request); AModelandview model =NewModelandview ("Test"); atModel.addobject ("Welcome", Req.getmessage ("Welcome")); -         returnmodel; -     } -}
5. Effect display

Summarize the key points of SPRINGMVC internationalization:

1) Specify the location of the file to be translated by spring internationalization, and be aware that the path must be complete.

2) Specify the parameters of spring internationalization, which I used to be language, when configured, spring will find the corresponding configuration file according to the value of the parameter in the request.

3) The front page uses the spring tag, the code corresponding to the key in the configuration file, the recommended use of Chinese, in case it does not correspond to the default display code inside the content (need to specify the configuration in the Spring-mvc.xml)

4) Background code internationalization returns a string using the GetMessage method of the RequestContext object.

The above is my realization of SPRINGMVC internationalization, there are questions can leave a message.

The SPRINGMVC internationalization based on session

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.