SpringMVC4.3x教程之七国际化的三种实现详解

国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。开发这样的程序的过程,就称为国际化。—摘自百度百科
SpringMVC也是支持国际化的操作,主要是前端控制器内部拥有国际化解析器
SpringMVC的国际化的支持方式:
1、Accept-Language
控制器无需写额外的内容,是根据请求消息头自动获取
2、HttpSession
使用Session传输语言环境
3、Cookie
使用Cookie传送语音环境


第一种基于Accept-Language实现


使用步骤:
1、创建国际化配置文件,存放到src下
英文
message_en_US.properties:
内容:

labeltime=Current Time

中文
message_zh_CN.properties:
内容:

labeltime=当前时间

2、设置配置文件

<!--配置国际化文件的格式:加载指定格式开头的文件  -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <!--多个文件开头用basenames,文件开头都相同用basename-->
    <!-- <property name="basenames">
    <list>
    <value>message</value>
    </list>
    </property> -->
    <property name="basename" value="message"/>
    </bean>
    <!--国际化实现的第一种方式:借助消息头实现:Accept-Language  -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"></bean>

3、页面中使用
通过SpringMVC的message获取国际化文本内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--配置标签库  -->
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>国际化的实现</title>
</head>
<body>
<!--使用Spring MVC的国际化支持, spring:message可以加载国际化文件的key -->
<h1><spring:message code="labeltime"/>:${time}</h1>
</body>
</html>

上述在火狐浏览器测试最好用,直接设置内容的语言就可以,无需重启,选项—-内容—语言—上移想要的语言

第二种使用HttpSession实现国际化

首先配置:
需要将之前配置的第一种给注释掉

<!--如果国际化是通过Session或Cookie实现的,那么需要配置拦截器  -->
    <mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>
    <!--国际化实现的第二种方式:借助HttpSession  -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

控制器:


//国际化控制器
@Controller
public class LanguageController {

    @ModelAttribute
    public void test4(Model model) {
        model.addAttribute("time",
                new SimpleDateFormat("yyyy/MM/dd HH:mm:ss SSS").format(Calendar.getInstance().getTime()));
    }
    // 基于Session的实现
    @RequestMapping("/gjhBySession")
    public String test2(String lge, HttpSession session) {
        Locale locale;// 国际化语言信息
        System.out.println(lge);
        switch (lge) {
        case "zh":// 中文
            // 参数说明:1、语言2、国家
            locale = new Locale("zh", "CN");
            break;
        case "en":// 英文
            locale = new Locale("en", "US");
            break;
        default:
            // 获取默认的国际化
            locale = LocaleContextHolder.getLocale();
            break;
        }

        // 设置属性标记当前的语言环境
    session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
        return "index";
    }
}

页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--配置标签库  -->
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>国际化的实现</title>
</head>
<body>
<div style="float:right">
<a href="gjhBySession?lge=zh">中文</a>|<a href="gjhBySession?lge=en">英文</a>
</div>
<h1><spring:message code="labeltime"/>:${time}</h1>
</body>
</html>

第三种基于Cookie实现国际化


配置文件

<!--国际化实现的第一种方式:借助消息头实现:Accept-Language  -->
    <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"></bean> -->
    <!--如果国际化是通过Session或Cookie实现的,那么需要配置拦截器  -->
    <mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>
    <!--国际化实现的第三种方式:借助Cookie  -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"></bean>

控制器:

//国际化控制器
@Controller
public class LanguageController {
    @Autowired
    private CookieLocaleResolver localeResolver;
    @ModelAttribute
    public void test4(Model model) {
        model.addAttribute("time",
                new SimpleDateFormat("yyyy/MM/dd HH:mm:ss SSS").format(Calendar.getInstance().getTime()));
    }
    //基于Cookie的实现
    @RequestMapping("/gjhByCookie")
    public String test3(String lge, HttpServletRequest request,HttpServletResponse response) {
        Locale locale;// 国际化语言信息
        System.out.println(lge);
        switch (lge) {
        case "zh":// 中文
            // 参数说明:1、语言2、国家
            locale = new Locale("zh", "CN");
            break;
        case "en":// 英文
            locale = new Locale("en", "US");
            break;
        default:
            // 获取默认的国际化
            locale = LocaleContextHolder.getLocale();
            break;
        }
        //CookieLocaleResolver localeResolver=new CookieLocaleResolver();
        localeResolver.setLocale(request, response, locale);
        return "index";
    }
}

页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--配置标签库  -->
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>国际化的实现</title>
</head>
<body>
<div style="float:right">
<a href="gjhByCookie?lge=zh">中文</a>|<a href="gjhByCookie?lge=en">英文</a>
</div>
<h1><spring:message code="labeltime"/>:${time}</h1>
</body>
</html>

上述就是SpringMVC实现国际化的三种方式的详细代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值