Spring 国际化资源
以前,在学习Struts2的时候经常听到关于国际化的消息。记得Struts2国际化的时候我们需要在cmd下面编译资源文件,但是Spring在资源国际化的时候则不需要这么麻烦。而且如果你是使用Eclipse或者MyEclipse的话,当你新建好message_en_US.properties、message_zh_CN.properties资源文件之后。如果是在
message_zh_CN.properties文件中,当你输入中文的时候其会自动将其转换为unicode编码。这样就省去了cmd下的编译国际化。相对而言还是挺方便的。
使用Eclipse或者MyEclipse的时候,
message_en_US.properties、message_zh_CN.properties资源文件必须放在src的目录下,否则编译的时候找不到资源文件。
下面给一个具体例子吧:
package com.clark;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class UserBean implements InitializingBean, DisposableBean {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init() {
this.username = "zhangsan";
this.age = 20;
}
public void clear() {
this.username = "";
this.age = 0;
}
//销毁
public void destroy() throws Exception {
this.username = "";
this.age = 0;
}
//在调用properties文件后初始化设值
public void afterPropertiesSet() throws Exception {
this.username = "wangwu";
this.age = 20;
}
}
package com.clark;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
//测试类
public class Test {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
UserBean userBean=(UserBean)context.getBean("user");
System.out.println("username="+userBean.getUsername());
System.out.println("age="+userBean.getAge());
//之所以使用Object[]数组,是因为其资源配置文件.properties中对应的参数有两个
Object[] objs=new Object[]{userBean.getUsername(),new Date().toString()};
//获取资源文件,Locale.CHINA指定采用何种语言
String str=context.getMessage("www.clark.com", objs, Locale.CHINA);
System.out.println(str);
}
}
资源文件是:message_en_US.properties下为:
www.clark.com=welcome {0},now time is\:{1}
message_zh_CN.properties中为:(最原始为www.clark.com=欢迎 {0},现在时间是:{1})------这就是编译器的强大所在
www.clark.com=\u6B22\u8FCE {0},\u73B0\u5728\u65F6\u95F4\u662F\:{1}
applicationContext.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="user" class="com.clark.UserBean"
abstract="false" singleton="true"
lazy-init="default" autowire="default"
dependency-check="default" destroy-method="clear">
</bean>
<!-- 配置国际化 配置messageSource这个bean(注意:一定是messageSource
而不是messageResource ,这是Spring规定的),而且资源配置*_en_US.properties等文件必须位于src目录下
否则,加载的时候找不到,会报错
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<!-- 国际化资源文件.properties中对应的key名称 -->
<property name="basenames">
<list>
<!-- 此值对应资源文件的名称message_en_US中的message必须与其对应 -->
<value>message</value>
</list>
</property>
</bean>
</beans>
package com.clark;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class UserBean implements InitializingBean, DisposableBean {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init() {
this.username = "zhangsan";
this.age = 20;
}
public void clear() {
this.username = "";
this.age = 0;
}
//销毁
public void destroy() throws Exception {
this.username = "";
this.age = 0;
}
//在调用properties文件后初始化设值
public void afterPropertiesSet() throws Exception {
this.username = "wangwu";
this.age = 20;
}
}
package com.clark;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
//测试类
public class Test {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
UserBean userBean=(UserBean)context.getBean("user");
System.out.println("username="+userBean.getUsername());
System.out.println("age="+userBean.getAge());
//之所以使用Object[]数组,是因为其资源配置文件.properties中对应的参数有两个
Object[] objs=new Object[]{userBean.getUsername(),new Date().toString()};
//获取资源文件,Locale.CHINA指定采用何种语言
String str=context.getMessage("www.clark.com", objs, Locale.CHINA);
System.out.println(str);
}
}
资源文件是:message_en_US.properties下为:
www.clark.com=welcome {0},now time is\:{1}
message_zh_CN.properties中为:(最原始为www.clark.com=欢迎 {0},现在时间是:{1})------这就是编译器的强大所在
www.clark.com=\u6B22\u8FCE {0},\u73B0\u5728\u65F6\u95F4\u662F\:{1}
applicationContext.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="user" class="com.clark.UserBean"
abstract="false" singleton="true"
lazy-init="default" autowire="default"
dependency-check="default" destroy-method="clear">
</bean>
<!-- 配置国际化 配置messageSource这个bean(注意:一定是messageSource
而不是messageResource ,这是Spring规定的),而且资源配置*_en_US.properties等文件必须位于src目录下
否则,加载的时候找不到,会报错
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<!-- 国际化资源文件.properties中对应的key名称 -->
<property name="basenames">
<list>
<!-- 此值对应资源文件的名称message_en_US中的message必须与其对应 -->
<value>message</value>
</list>
</property>
</bean>
</beans>