spring 版本4.1.2
loginFrom.username=login username loginFrom.password=login password locale.selection = Language Selectionmessages_zh_CN.properties 中文简体
loginFrom.username=\u767B\u9646\u7528\u6237\u540D loginFrom.password=\u767B\u9646\u5BC6\u7801 locale.selection = \u8BED\u8A00\u9009\u62E9messages_zh.properties 中文
loginFrom.username=\u767B\u9646\u7528\u6237\u540D zh loginFrom.password=\u767B\u9646\u5BC6\u7801 zh locale.selection = \u8BED\u8A00\u9009\u62E9 zh
<!-- 国际化 --> <!-- xx?locale=en_US 自动修改语言的拦截器的拦截器 <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieName" value="locale" /> <property name="cookieMaxAge" value="604800" /> <property name="defaultLocale" value="zh" /> </bean> --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <!-- src/messages --> <value>messages/messages</value> </list> </property> <!-- 如果没有找到国际化key值 默认输出key --> <property name="useCodeAsDefaultMessage" value="true" /> </bean>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<spring:message code="loginFrom.username"/> <spring:message code="loginFrom.password"/>
<@spring.message code="loginFrom.username"/> 向页面输出国际化信息
<script type="text/javascript" src="/18n/js/jquery-1.7.2.js"></script> <#import "/common/spring.ftl" as spring/> <h1>login.ftl </h1> <hr/> <select id="selection"> <option><@spring.message code="locale.selection"/></option> <option value="en_US">English</option> <option value="zh_CN">中文简体</option> <option value="zh">中文</option> </select><hr/> <from> <@spring.message code="loginFrom.username"/><input type="text"/><br/> <@spring.message code="loginFrom.password"/><input type="text"/><br/> </from> <hr/> <script type="text/javascript"> $(function(){ $("#selection").change(function(){ var locale = $(this).val(); $.post("/18n/locale/change",{locale:locale},function(data){ if(data == "success"){ window.location.href="/18n/locale/login" }else{ alert("错误"); } },"html") }); }) </script>
package com.lwp.controller; import java.io.IOException; import java.util.Locale; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.support.RequestContext; @Controller @RequestMapping("/locale") public class LocaleController { @RequestMapping("/login") public String login(){ return "/login"; } @Autowired private LocaleResolver localeResolver; @RequestMapping("/change") @ResponseBody public String setSession(HttpServletRequest request,HttpServletResponse response,String locale){ try { //这两个配置任选其一,可以不加if判断删除其一 我这里是任意配置session或者cookie修改配置后不需变代码 //<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> if(localeResolver instanceof org.springframework.web.servlet.i18n.SessionLocaleResolver){ String[] localeArr = locale.split("_"); Locale l = null; if(localeArr.length == 1){ l = new Locale(localeArr[0]); }else{ l = new Locale(localeArr[0],localeArr[1]); } localeResolver.setLocale(request, response, l); //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, l); //功能同上 System.out.println("SessionLocaleResolver"); } //<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> if(localeResolver instanceof org.springframework.web.servlet.i18n.CookieLocaleResolver){ Cookie cookie = new Cookie(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME, new Locale(locale).toString()); cookie.setMaxAge(5); response.addCookie(cookie); response.flushBuffer(); System.out.println("CookieLocaleResolver"); } } catch (Exception e) { e.printStackTrace(); return "error"; } return "success"; } /** * 后台根据国际化编码返回信息 * @param response * @param request */ @RequestMapping("returnMessage") public void returnMessage(HttpServletResponse response,HttpServletRequest request){ RequestContext rc = new RequestContext(request); String w = rc.getMessage("loginFrom.username"); //根据locale不同返回不同语言 try { response.getWriter().write(w); } catch (IOException e) { e.printStackTrace(); } } }