Spring温习(8)--国际化的支持
我今天介绍的国际化呢,平时基本上都用不上,我的项目中也从来没用过,就记得当初学的时候有这么一部分,就回顾比较浅显的一部分了,以备今后使用
首先呢,必须要在Spring配置文件中配置这么一段
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。这里的Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找。 这样,我们便可以在程序中通过ApplicationContext 对象调用getMessage()方法获取资源文件的信息。--> <bean id="messageSource" lazy-init="default" autowire="default" dependency-check="default"> <property name="basenames"> <list> <value>config/properties</value> </list> </property> </bean></beans>
?
两个资源文件
properties_en_US.properties
www.iteye.com=welcome {0},time:{1}
?
properties_zh_CN.properties
www.iteye.com=\u6B22\u8FCE{0},\u65F6\u95F4\:{1}
?
至于\u6B22\u8FCE和\u65F6\u95F4\其实就是‘欢迎’和‘时间’的ASCII码值,在国际化资源文件中,中文汉字必须要配置成ASCII形式的,具体怎么得到这些值的呢
win+r-->运行窗口 输入native2ascii (前提是装了JDK了)可得到如下界面了

?只要输入汉字,然后回车就得到ASCII值
?
好了,来测试一下
package com.javacrazyer.test;import java.util.Date;import java.util.Locale;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class CommonTest {@Testpublic void test1() {String path = "WebRoot/WEB-INF/applicationContext.xml";ApplicationContext context = new FileSystemXmlApplicationContext(path);Object[] objs = new Object[] { "javacrazyer",new Date().toLocaleString() };// www.eimhe.com为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言String str = context.getMessage("www.iteye.com", objs, Locale.CHINA);System.out.println(str);}}?
输出结果为
欢迎javacrazyer,时间:2010-9-3 11:00:22
由于代码中指明了语言环境是CHINA,所以会自动去匹配查找zh_CN的资源文件
?
在WEB页面上怎么实现呢,首先添加个sping.tld到WEB-INF/lib下,还需要spring-webmvc.jar
在这之后需在Spring配置文件中添上
<bean id="localeResolver" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%><html> <head> <title>Spring国际化</title> </head> <body> <spring:message code="www.iteye.com" /><br> <input type="button" value="<spring:message code="www.iteye.com" />"/><br> </body></html>
?
由于我们浏览器默认都是中文的,所以显示的结果肯定是中文的啦,怎样显示英文呢,只要更换下浏览器语言即可

?选择好美国英语之后确定,刷新下页面,页面的就变成英文的啦
?
web.xml中还是老样子,最简单的配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- Spring ApplicationContext Definition --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
?
?
?
?
?
?
?
?