您的位置:首页 > 编程语言 > Java开发

SpringMVC-国际化

2016-06-07 22:08 417 查看
国际化
-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>


<完>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: