spring 国际化(1)
greeting.morning=good morning!
application_zh_CN.properties
greeting.morning=\u65e9\u4e0a\u597d
application_ja_JP.properties(我电脑是日文系统--本地系统默认的本地化对象对应的资源)
greeting.morning=\u304a\u65e9\u3046\u3054\u3056\u3044\u307e\u3059
application.properties(默认的资源)
greeting.morning=good morning!
?
2,配置资源文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id="resource" value="5"/><!-- 资源刷新间隔时间 --> </bean></beans>
?3,测试
package spring3.local;import java.util.Locale;import org.springframework.context.ApplicationContext;import org.springframework.context.MessageSource;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) throws InterruptedException {ApplicationContext context = new ClassPathXmlApplicationContext("spring3/local/resource.xml");// 运行时不需要刷新资源文件可以用下面方式MessageSource resource = (MessageSource) context.getBean("resource");System.out.println("CHINA:"+ resource.getMessage("greeting.morning", null, Locale.CHINA));System.out.println("JAPAN:"+ resource.getMessage("greeting.morning", null, Locale.JAPAN));System.out.println("US:"+ resource.getMessage("greeting.morning", null, Locale.US));System.out.println("CANADA:"+ resource.getMessage("greeting.morning", null, Locale.CANADA));System.out.println();// 运行时想不停止服务刷新资源文件可以用下面方式resource = (MessageSource) context.getBean("reloadableResource");System.out.println("修改前US:"+ resource.getMessage("greeting.morning", null, Locale.US));// 修改文件application_en_US.properties// greeting.morning=good morning!// -->greeting.morning=good night!Thread.sleep(20000);System.out.println("修改后US:"+ resource.getMessage("greeting.morning", null, Locale.US));}}
?测试结果:
CHINA:早上好JAPAN:お早うございますUS:good morning!CANADA:お早うございます修改前US:good morning!修改后US:good night!
?此处可以说明:
(1),canada对应的资源文件不存在,他按照前面红字所示顺序首先查找我本机的locale对应的资源文件,如果找到,则输出这个资源文件中的信息。
(2),用ReloadableResourceBundleMessageSource这个类时,确实没有重新启动服务而刷新了资源文件的信息。
?
4,我们继续实验,将日文的资源文件删除掉后。
测试结果
CHINA:早上好JAPAN:good morning!US:good morning!CANADA:good morning!修改前US:good morning!修改后US:good night!
?可以看到,日文和加拿大的输出都变成了默认资源文件中的值。
?
5,我们再次实验,把默认资源文件删除掉。
CHINA:早上好3 19, 2013 4:00:54 午後 org.springframework.context.support.ResourceBundleMessageSource getResourceBundle警告: ResourceBundle [spring3/local/application] not found for MessageSource: Can't find bundle for base name spring3/local/application, locale ja_JPException in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'greeting.morning' for locale 'ja_JP'.
?报出异常了。
?
从而可以验证资源文件的查找顺序。
①指定locale对应的资源文件,存在则结束,不存在往下继续查找
②本机对应的locale对应的资源文件,存在则结束,不存在往下继续查找
③默认指定的资源文件,存在则结束,不存在往下继续查找
④上记都不存在时,抛出异常。