1、资源文件message_zh_CN.properties和message_en_US.properties
message_zh_CN.properties:
HelloWorld={0},现在是{1}
message_en_US.properties:
HelloWorld={0},now is {1}
2、 applicationContext.xml的配置
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<!-- 这里我的资源文件的路径是com/homework/local -->
<value>com/homework/local/message</value>
</list>
</property>
</bean>
3、测试
package com.homework.app;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringEnvTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml");
Object[] objects = new Object[]{"HelloWorld",new Date()};
// String message= ctx.getMessage("HelloWorld",objects,Locale.CHINA);
String message= ctx.getMessage("HelloWorld",objects,Locale.US);
System.out.println(message);
}
}
getMessage是这样的:
第一个参数HelloWorld-找到资源文件中name为HelloWorld的那条-value是 “{0},现在是{1}”
第二个参数objects是Object数组,而这个数组中的“HelloWorld”匹配value中的{0},new Date()匹配value中的{1}
第三个参数是指定什么语言