zoukankan      html  css  js  c++  java
  • spring mvc 国际化

    spring mvc 国际化

    使用CookieLocaleResolver实现国际化的步骤:
    1.注册 messageSource,localeResolver 两个bean
    2.调用localeResolver.setLocale()可改变 Locale
    3.RequestContext.getMessage()ApplicationContext.getMessage()可以获取国际化信息
    4.jsp页面引入spring标签后,也可直接获取国际化信息

    spring 配置文件:

     1     <!-- 配置国际化资源文件路径 -->
     2     <bean id="messageSource"
     3         class="org.springframework.context.support.ResourceBundleMessageSource">
     4         <property name="basename" value="MessageBundle" /><!-- MessageBundle_en_US.properties,MessageBundle_zh_CN.properties -->
     5         <property name="defaultEncoding" value="UTF-8" />
     6     </bean>
     7     <!-- 注册spring mvc的cookie locale,其他还有SessionLocaleResolver等 -->
     8     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
     9         <property name="cookieName" value="language" />
    10         <property name="cookieMaxAge" value="604800" /><!-- cookieMaxAge属性表示这个Cookie应该持续多少秒,-1表示这个Cookie在浏览器关闭之后就失效 -->
    11         <property name="defaultLocale" value="zh_CN" />
    12     </bean>
    View Code

    Controller中配置:

     1 @Controller
     2 public class LoginController {
     3     @Autowired
     4     private LocaleResolver localeResolver;
     5     @Autowired  
     6     private ApplicationContext appCtx;
     7 
     8     @RequestMapping(value = "changeLocale")
     9     public String changeLocale(String locale,HttpServletRequest request,HttpServletResponse response) {
    10         
    11         //locale 格式为 language_country,例如 en_US,zh_CN
    12         Locale setLocale = new Locale(locale.split("_")[0] , locale.split("_")[1]);
    13         localeResolver.setLocale(request, response, setLocale);
    14         
    15         //设置变量 locale 为当前 Locale
    16         request.setAttribute("locale", localeResolver.resolveLocale(request));
    17         
    18         //按当前 Locale 获取国际化信息
    19         RequestContext requestContext = new RequestContext(request);        
    20         System.out.println(requestContext.getMessage("username"));
    21         
    22         //按指定 Locale 获取国际化信息
    23         System.out.println(appCtx.getMessage("username", null, Locale.CHINA));
    24         System.out.println(appCtx.getMessage("username", new Object[]{}, Locale.US));
    25         
    26         return "locale";
    27         //return "redirect:/";//跳转回原页面
    28     }
    29 }
    View Code


    locale.jsp代码:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <%@ taglib prefix="s" uri="http://www.springframework.org/tags" %><!-- 引入spring标签 -->
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 </head>
     7 <body>
     8     <s:message code='username'/>
     9     <s:message code="hello" arguments="${locale.language},${locale.country}" argumentSeparator=","/>
    10     <!-- arguments是用来给资源文件添加参数的,argumentSeparator是用来分割多个参数的标记  -->
    11     ${locale}:${locale.language}_${locale.country}
    12 </body>
    13 </html>
    View Code

    MessageBundle_en_US.properties:

    1 username=guodefu
    2 hello=hi,locale.language is {0},country is {1}

    MessageBundle_zh_CN.properties:

    1 username=郭德福
    2 hello=嗨,当前locale的语言是{0},国家是{1}

    查看结果:
    http://localhost:8080/guoguo-maven-web/changeLocale?locale=en_US
    http://localhost:8080/guoguo-maven-web/changeLocale?locale=zh_CN

  • 相关阅读:
    【手绘漫画】图解LeetCode之x 的平方根(LeetCode 69题)
    tcp 发送长度9 实际组包49
    tcp 发送报文长度和响应报文长度
    http 响应报文
    中台翻车纪实:一年叫停,员工转岗被裁,资源全浪费
    再也不怕女朋友问我二分查找了!【手绘漫画】图解二分查找(修订版)(LeetCode 704题)
    我的Hexo-Github博客搭建笔记
    J
    怎样使用npm打包公布nodejs程序包
    JQuery Jcrop—JQuery Jcrop 图像裁剪工具学习
  • 原文地址:https://www.cnblogs.com/guodefu909/p/4885926.html
Copyright © 2011-2022 走看看