I18N国际化
一、在spring.xml中配置
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 12 <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> 13 <mvc:annotation-driven /> 14 <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> 15 <context:component-scan base-package="com.**.**" /> 16 <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /> 18 19 <!-- 根据浏览器语言判断start --> 20 <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> --> 21 <!-- 根据浏览器语言判断end --> 22 <!-- ******************************************************************************** --> 23 <!-- 根据session判断start --> 24 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> 25 <mvc:interceptors> 26 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 27 </mvc:interceptors> 28 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 29 <!-- 根据session判断end --> 30 <!-- ******************************************************************************** --> 31 <!-- 根据cookie判断start --> 32 <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> 33 <mvc:interceptors> 34 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 35 </mvc:interceptors> 36 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> 37 <!-- 根据cookie判断end --> 38 <!-- ******************************************************************************** --> 39 40 </beans>
二、创建三个jsp页面
1.根据cookie判断language_cookie.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <a href="language_cookie?langType=zh">中文</a> | <a href="language_cookie?langType=en">英文</a><br/> 14 下面展示的是后台获取的国际化信息:<br/> 15 ${text}<br/> 16 17 页面获取信息:<br/> 18 <spring:message code="text"/> <br/> 19 Locale: ${pageContext.response.locale } 20 </body> 21 </html>
2.根据浏览器当前语言判断language_liulanqi.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>根据浏览器语言获取</title> 11 </head> 12 <body> 13 后台获取的国际化信息:<br/> 14 ${text}<br/> 15 16 页面获取信息:<br/> 17 <spring:message code="text"/> <br/> 18 Locale: ${pageContext.response.locale } 19 </body> 20 </html>
3.根据session判断language_session.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <a href="language_session?langType=zh">中文</a> | <a href="language_session?langType=en">英文</a><br/> 14 下面展示的是后台获取的国际化信息:<br/> 15 ${text}<br/> 16 17 页面获取信息:<br/> 18 <spring:message code="text"/> <br/> 19 Locale: ${pageContext.response.locale } 20 </body> 21 </html>
三、配置测试类GlobalController.java
1 package com.sh.test; 2 3 4 import java.util.Locale; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.context.i18n.LocaleContextHolder; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RequestMethod; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.servlet.ModelAndView; 15 import org.springframework.web.servlet.i18n.CookieLocaleResolver; 16 import org.springframework.web.servlet.i18n.SessionLocaleResolver; 17 import org.springframework.web.servlet.support.RequestContext; 18 /** 19 * 需在spring-servlet.xml中开启对应的配置才能生效 20 * @author Administrator 21 * 22 */ 23 @Controller 24 public class GlobalController { 25 26 27 28 /** 29 * 根据浏览器语言判断 30 * @param request 31 * @param response 32 * @param langType 33 * @return 34 */ 35 @RequestMapping(value="/language_liulanqi", method = {RequestMethod.GET}) 36 public ModelAndView test(HttpServletRequest request,HttpServletResponse response){ 37 //从后台代码获取国际化信息 38 RequestContext requestContext = new RequestContext(request); 39 request.setAttribute("text",requestContext.getMessage("text")); 40 return new ModelAndView("/i18n/language_liulanqi"); 41 } 42 /** 43 * 根据session判断 44 * @param request 45 * @param response 46 * @param langType 47 * @return 48 */ 49 @RequestMapping(value="/language_session", method = {RequestMethod.GET}) 50 public ModelAndView test1(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="langType", defaultValue="zh") String langType){ 51 request.getSession().removeAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME); 52 if(langType.equals("zh")){ 53 Locale locale = new Locale("zh", "CN"); 54 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 55 } 56 else if(langType.equals("en")){ 57 Locale locale = new Locale("en", "US"); 58 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 59 }else{ 60 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale()); 61 } 62 //从后台代码获取国际化信息 63 RequestContext requestContext = new RequestContext(request); 64 String message = requestContext.getMessage("text"); 65 request.setAttribute("text",message); 66 return new ModelAndView("/i18n/language_session"); 67 } 68 /** 69 * 根据cookie判断 70 * @param request 71 * @param response 72 * @param langType 73 * @return 74 */ 75 @RequestMapping(value="/language_cookie", method = {RequestMethod.GET}) 76 public ModelAndView test2(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="langType", defaultValue="zh") String langType){ 77 if(langType.equals("zh")){ 78 Locale locale = new Locale("zh", "CN"); 79 (new CookieLocaleResolver()).setLocale (request, response, locale); 80 }else if(langType.equals("en")){ 81 Locale locale = new Locale("en", "US"); 82 (new CookieLocaleResolver()).setLocale (request, response, locale); 83 }else{ 84 (new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale()); 85 } 86 //从后台代码获取国际化信息 87 RequestContext requestContext = new RequestContext(request); 88 request.setAttribute("text",requestContext.getMessage("text")); 89 return new ModelAndView("/i18n/language_cookie"); 90 } 91 92 }
五、在src下创建i18n配置文件,
messages_en_US.properties
text=English2
messages_zh_CN.properties
text=中文
messages.properties
text=默认
六、使用方式
1.根据浏览器语言选择,需要打开配置文件中的对应配置,开启此模式后只能通过修改浏览器首选语言切换
http://192.168.1.168:8080/i18ntest/language_liulanqi
2.根据session选择,开启对应配置,通过URL参数切换语言
http://192.168.1.168:8080/i18ntest/language_session?langType=zh
3.根据cookie选择,开启对应配置,通过URL参数切换语言
http://192.168.1.168:8080/i18ntest/language_cookie?langType=en