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命令轉(zhuǎn)成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數(shù)組 內(nèi)容對應(yīng) 配置文件中的 {0} {1}參數(shù)
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數(shù)組 內(nèi)容對應(yīng) 配置文件中的 {0} {1}參數(shù)
System.out.println(msg);
}
}
發(fā)表于 @ 2009年03月22日 00:27:00