package javaee.ApplicationContext
; //这个是代码所在的源包相对路径
public class internationalization {
public static void main(String [] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("javaee/ApplicationContext/bean.xml
"); //注意xml文件所在的位置src/javaee/...
String[] a = {"读者"};
String hello = ctx.getMessage("hello", a, Locale.getDefault()
);//读取所在机子的默认语言
Object[] b = {new Date()};
String now = ctx.getMessage("hello", b, Locale.US
);//手动设置
System.out.println(hello);
System.out.println(now);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames
"> //注意
:list类型 name为basenames
<list>
<value>message</value>
</list>
</property>
//****
<property name="basename">
<value>...</value>
</property>
</bean>
</beans>
注意:message_en_US等资源文件要存放在SRC目录下,否则无法读取