il8n国际化
支持多国语言的web应用,根据客户端系统的语言类型返回对应的界面
方案
为每种语言提供一套相应的资源文件,并以规范化命名的方式保存在特定的目录中,由系统自动根据客户端语言选择适合的资源文件返回
JDK对国际化的支持
1、国际化信息,也称为本地化信息,一般需要两个条件可以确定,即语言类型和国家/地区类型。java通过java.util.Locale类表示一个本地化对象
@Test public void testLocale() { // 语言参数和国家/地区参数都使用ISO标准语言代码表示,每种语言由两位小写字母表示,每个国家/地区由两个大写字母表示 Locale locale = new Locale("zh", "CN"); } @Test public void testLocale1() { Locale locale = Locale.getDefault(); System.out.println(locale); // zh_CN }
2、java.util包中还提供了几个支持本地化工具类,如NumberFormat、DateFormat、MessageFormat
@Test public void numberFormat() { double amt = 123456.78; Locale locale = new Locale("zh", "CN"); NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale); System.out.println(currFmt.format(amt)); // ¥123,456.78 } @Test public void dateFormat() { Date date = new Date(); Locale locale = new Locale("en", "US"); DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); // 第一个参数是时间样式 System.out.println(df.format(date)); // Jan 17, 2019 } @Test public void messageFormat1() { // 以下两行是不同本地化的资源,{n}是占位符 String pattern1 = "{0},你好!你于{1}在工商银行存入{2} 元。"; String pattern2 = "At {1,time,short} On {1,date,long},{0} paid {2,number,currency}.";// short表示获取时分秒部分,long获取日期部分 // 将要替换的变量 Object[] params = {"John", new GregorianCalendar().getTime(),1.0E3}; // 中国格式 String msg1 = MessageFormat.format(pattern1,params); System.out.println(msg1); // John,你好!你于19-1-17 上午11:56在工商银行存入1,000 元。 // 美国格式 MessageFormat mf = new MessageFormat(pattern2, Locale.US); String msg2 = mf.format(params); System.out.println(msg2); // At 11:56 AM On January 17, 2019,John paid $1,000.00. }
国际化资源的命名规范
国际化资源的命名规范:资源名_语言代码_国家/地区代码.properties;资源名.properties是默认的资源文件,即系统按照命名规范在系统中找不到对应的文件时就使用默认的资源文件;资源名_语言代码.properties是某一语言的默认资源文件
示例:my_zh_CN.properties、my_en_US.properties
JDK ResourceBoundle 加载国际化资源
如果应用中有大量的本地化资源文件,直接通过File操作太笨拙。java提供了专门用于加载本地化资源文件的java.util.ResourceBoundle
1、资源文件
greeting.common=How are you! greeting.morning = Good morning! greeting.afternoon = Good Afternoon!
greeting.common=How are you! greeting.morning = Good morning! greeting.afternoon = Good Afternoon!
greeting.common=u60a8u597duff01 greeting.morning=u65e9u4e0au597duff01 greeting.afternoon=u4e0bu5348u597duff01
greeting.common=How are you! {0},today is {1} greeting.morning = Good morning! {0},now is {1,time,short} greeting.afternoon = Good Afternoon! {0} now is {1,date,long}
greeting.common=How are you! {0},today is {1} greeting.morning = Good morning! {0},now is {1,time,short} greeting.afternoon = Good Afternoon! {0}, now is {1,date,long}
greeting.common=u60a8u597duff01 {0}uff0cu4ecau5929u662f {1} greeting.morning=u65e9u4e0au597duff01 {0}uff0cu73b0u5728u662f{1,time,short} greeting.afternoon=u4e0bu5348u597duff01 {0}uff0cu73b0u5728u662f{1,date,long}
2、程序
@Test public void resourceBoundle(){ // 如果不指定本地化对象,将使用本地系统默认的本地化对象。 ResourceBundle rb1 = ResourceBundle.getBundle("resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("resource",Locale.CANADA); System.out.println("us:"+rb1.getString("greeting.common")); // us:How are you! System.out.println("cn:"+rb2.getString("greeting.common")); // cn:您好! } @Test // 在资源文件中使用占位符 public void resourceBoundleFmt(){ ResourceBundle rb1 = ResourceBundle.getBundle("fmt_resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("fmt_resource",Locale.CHINA); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = new MessageFormat(rb1.getString("greeting.common"),Locale.US).format(params); String str2 =new MessageFormat(rb2.getString("greeting.morning"),Locale.CHINA).format(params); String str3 =new MessageFormat(rb2.getString("greeting.afternoon"),Locale.CHINA).format(params); System.out.println(str1); System.out.println(str2); System.out.println(str3); }
Spring的MessageSource接口
Spring定义了访问国际化信息的MessageSource接口,MessageSource接口被HierarchicalMessageSource、ApplicationContext这两个接口扩展。
HierarchicalMessageSource添加了两个方法,setParentMessageSource()和getParentMessageSource(),用来设置父子层级的MessageSource。
HierarchicalMessageSource最重要的两个实现类是ResourceBundleMessageSource、ReloadableResourceBundleMessageSource,它们基于java的ResourceBundle实现。ReloadableResourceBundleMessageSource提供了定时刷新功能,在不重启系统的情况下可以更新资源。
ApplicationContext也实现了MessageSource接口,在AbstractApplicationContext的refresh()方法中调用initMessageSource()就是初始化容器中的国际化信息资源,它根据反射机制从BeanDefinitionRegistry中找出名为messageSource且类型为org.springframework.context.MessageSource的bean,并将这个bean定义的信息资源加载为容器级的国际化信息资源,假设bean没有被命名为messageSource,会抛出异常。
资源文件使用上面的properties文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myResource" class="org.springframework.context.support.ResourceBundleMessageSource"> <!-- basenames要写死,父类源码里有这个属性 --> <property name="basenames"> <list> <value>fmt_resource</value> </list> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myResource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- basenames要写死,父类源码里有这个属性 --> <property name="basenames"> <list> <value>fmt_resource</value> </list> </property> <property name="cacheSeconds" value="5"/> </bean> </beans>
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- id的值messageSource是写死的,默认为找它 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- basenames要写死,父类源码里有这个属性 --> <property name="basenames"> <list> <value>fmt_resource</value> </list> </property> </bean> </beans>
@Test public void TestResourceBundleMessageSource() throws InterruptedException { //ApplicationContext context = new ClassPathXmlApplicationContext("xml/my.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("xml/my2.xml"); MessageSource messageSource = context.getBean("myResource", MessageSource.class); // 数据 Object[] params = {"John", new GregorianCalendar().getTime()}; // 得到了最终的字符串 String message1 = messageSource.getMessage("greeting.common", params, Locale.US); String message2 = messageSource.getMessage("greeting.morning", params, Locale.CHINA); String message3 = messageSource.getMessage("greeting.afternoon", params, Locale.CHINA); System.out.println(message1); System.out.println(message2); System.out.println(message3); } @Test public void TestApplication() throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext("xml/my3.xml"); // 下面这行代码是可以省略的,因为默认加载messageSource的bean;当然不省略也可以 //MessageSource messageSource = context.getBean("messageSource", MessageSource.class); // 数据 Object[] params = {"John", new GregorianCalendar().getTime()}; // 得到了最终的字符串 String message1 = context.getMessage("greeting.common", params, Locale.US); String message2 = context.getMessage("greeting.morning", params, Locale.CHINA); String message3 = context.getMessage("greeting.afternoon", params, Locale.CHINA); System.out.println(message1); System.out.println(message2); System.out.println(message3); }
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>