SpringMVC-国际化

本文介绍如何在SpringMVC中进行国际化配置,包括通过不同方式确定本地化类型的方法,并演示了如何通过超链接切换语言。
国际化

-AcceptHeaderLocaleResolver:根据HTTP请求头的Accept-Language参数确定本地化类型,如果没有显示定义本地化解析器,SpringMVC使用该解析器。
-CookieLocaleResolver:根据指定的Cookie值确定本地化类型
-SessionLocaleResolver:根据Session中特定的属性来确定本地化类型
-LocaleChangeInterceptor:从请求参数中获取本次请求的本地化类型

1.根据浏览器语言设置,默认选择执行本地化操作。

JSTL的fmt标签实现。
<servlet-name>-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<context:component-scan base-package="spring"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="spring.i18n"></property>
	</bean>
	
	<mvc:view-controller path="/i18n" view-name="i18n"/>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

在上面指定了国际化文档的basename名字为 spring.i18n后,在spring目录下创建i18n对应的配置文件。
注意i18n_xx.properties的目录若放置错误,会报错,ResourceBundle [spring.i18n] not found for MessageSource

1)i18n_en_US.properties

i18n.name=Name
i18n.age=Age

2)i18n_zh_CN.properties //命名中文的名字,年龄

i18n.name=\u540D\u5B57 
i18n.age=\u5E74\u9F84

配置好后,就可以在对应的jsp文档中使用数据。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fmt:message key="i18n.name"></fmt:message>
	<fmt:message key="i18n.age"></fmt:message>
</body>
</html>

2.通过超链接切换本地化。

配置LocalResolver,和LocaleChangeInterceptor来实现。
由于新版本的SpringMVC进行了更新,如果直接使用SessionLocaleResolver会报如下错误,所以真正实现通过超链接切换中英文,需要通过子类覆盖报错父类方法。
java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy

在上面的配置文件基础上追加如下配置

	<bean id="localResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
	</mvc:interceptors>
jsp代码实现如下:
注意locale需要全部小写。
	<a href="i18n?locale=zh_CH">中文</a><br>
	<a href="i18n?locale=us_EN">英文</a><br>
	
	<fmt:message key="i18n.name"></fmt:message>
	<fmt:message key="i18n.age"></fmt:message>

<完>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值