springmvc 基于cookie实现国际化

基于Cookie的国际化配置是指,检验用户浏览器中的Cookie,用CookieLocaleResolver来解析区域,实现国际化。
举个例子:要求显示一个表格,可以实现中英文的切换。

在spring mvc的配置文件配置国际化

打开springmvc.xml,加入以下代码:

 <!-- 语言国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
   <!-- 国际化信息对应的文件名 --> 
   <property name="basename" value="i18n" /> 
   <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 --> 
   <property name="useCodeAsDefaultMessage" value="true" /> 
</bean> 
<mvc:interceptors> 
   <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 
   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 
</mvc:interceptors> 
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

创建国际化的资源文件

在src下创建名为i18n_zh_CN.properties和i18n_en_US.properties,具体如下,
i18n_zh_CN.properties:

page.title = \u6D4B\u8BD5\u56FD\u9645\u5316
table.name = \u59D3\u540D
table.gerdar = \u6027\u522B
table.age = \u5E74\u9F84
table.class = \u73ED\u7EA7

i18n_en_US.properties:

page.title = Test nationalization
table.name = name
table.gerdar = gerdar
table.age = age
table.class = class

创建Controller类

代码如下:

package com.hzit.i18n;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestI18nController {
    // 基于Cookie的国际化处理
    @RequestMapping(value = "/changeLang")
    public String changeLang() {
        System.out.println(">>>"+LocaleContextHolder.getLocale());// 打印当前的本地化对象
        return "dis_table";
    }

}

应用页面jsp

代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!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><spring:message code="page.title"></spring:message></title>
<style>
table{width:50%;border-spacing:0;}
td{padding:5px 10px;border:1px solid #ccc;}
</style>
</head>
<body>
当前语言: ${pageContext.response.locale }<br><br>
选择语言:<a href="?locale=zh_CN">中文</a> | <a href="?locale=en_US">英文</a><br><br>
    <table>
        <tr>
            <td width="25%"><spring:message code="table.name"></spring:message></td>
            <td><input/></td>
        </tr>
        <tr>
            <td><spring:message code="table.gerdar"></spring:message></td>
            <td><input/></td>
        </tr>
        <tr>
            <td><spring:message code="table.age"></spring:message></td>
            <td><input/></td>
        </tr>
        <tr>
            <td><spring:message code="table.class"></spring:message></td>
            <td><input/></td>
        </tr>
    </table>
</body>
</html>

总结

基于cookie的方式和基于session的方式有点类似,只不过LocaleResolver的实现不同,session使用的是SessionLocaleResolver,而cookie使用的是CookieLocaleResolver。若两者对应的本地化对象没有,则会根据浏览来解析用户区域。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值