1.在config.xml文件中加入bean
<?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="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>welcome</value>
</property>
</bean>
</beans>
<?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="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>welcome</value>
</property>
</bean>
</beans>
其中的id 值一定是messageSource ,类是 org.springframework.context.support.ResourceBundleMessageSource
basename属性的值是你存放国际化信息的文件名的前缀.
2.写porperties国际化文件~~~~并把非英文的文件用native2ascii命令转成utf-8类型
(最好是通过Eclipse的国际化插件做)
poperties文件放在src目录下
welcome_en_US.properties 英语
HelloWorld=language: {0} time: {1}
HelloWorld=language: {0} time: {1}
welcome_zh_CN.properties 中文
原文: HelloWorld=问候语: {0} 问候时间: {1}
view plaincopy to clipboardprint?
HelloWorld=\u95ee\u5019\u8bed: {0} \u95ee\u5019\u65f6\u95f4: {1}
HelloWorld=\u95ee\u5019\u8bed: {0} \u95ee\u5019\u65f6\u95f4: {1}
3. 写程序
package com.zhao.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.Calendar;
import java.util.Locale;
public class TestHelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
Object[] objs = new Object[] {"Hello",Calendar.getInstance().getTime()};
//Calendar.getInstance().getTime() 得到时间
String msg = actx.getMessage("HelloWorld", objs, Locale.CHINA); //中文
//String msg = actx.getMessage("HelloWorld", objs, Locale.US); //英文
//objs数组 内容对应 配置文件中的 {0} {1}参数
System.out.println(msg);
}
}
package com.zhao.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.Calendar;
import java.util.Locale;
public class TestHelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
Object[] objs = new Object[] {"Hello",Calendar.getInstance().getTime()};
//Calendar.getInstance().getTime() 得到时间
String msg = actx.getMessage("HelloWorld", objs, Locale.CHINA); //中文
//String msg = actx.getMessage("HelloWorld", objs, Locale.US); //英文
//objs数组 内容对应 配置文件中的 {0} {1}参数
System.out.println(msg);
}
}
发表于 @ 2009年03月22日 00:27:00