1.Java本身框架国际化
(1)本地化相关的类
Locale
NumberFormat
DateFormat
MessageFormat
(2)使用ResourceBoundle
国际化资源命名规范:
<资源名>_<语言代码>_<国家/地区代码>.properties
如:
resource.properties
resource_zh_CN.properties
resource_en_US.properties
使用例子
package com.baobaotao.i18n; import java.text.DateFormat; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.ResourceBundle; public class LocaleSample { public static void numberFormat() { Locale locale = new Locale("zh", "CN"); NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale); double amt = 123456.78; System.out.println(currFmt.format(amt)); } public static void dateFormat() { Locale locale = new Locale("en", "US"); Date date = new Date(); DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); System.out.println(df.format(date)); } public static void messageFormat1() { Object[] params = {"John", new GregorianCalendar().getTime(),1.0E3}; String pattern1 = "{0},你好!你于{1}在工商银行存入{2} 元。"; String pattern2 = "At {1,time,short} On{1,date,long},{0} paid {2,number,currency}."; String msg1 = MessageFormat.format(pattern1,params); MessageFormat mf = new MessageFormat(pattern2,Locale.US); String msg2 = mf.format(params); System.out.println(msg1); System.out.println(msg2); } public static void resourceBoundle(){ ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/resource",Locale.CANADA); System.out.println("us:"+rb1.getString("greeting.common")); System.out.println("cn:"+rb2.getString("greeting.common")); } public static void resourceBoundleFmt(){ ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/fmt_resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/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); } public static void main(String[] args) { numberFormat(); dateFormat(); messageFormat1(); resourceBoundle(); resourceBoundleFmt(); } }
resource.properties
greeting.common=How are you! greeting.morning = Good morning! greeting.afternoon =Good Afternoon\!
fmt_resource.properties
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}
2.Spring国际化
相关的接口及类:
(1)接口:MessageSource
(2)类:ResourceBundleMessageSource
通过beanName指定一个资源名
(3)类:ReloadableResourceBundleMessageSource
可以定时刷新资源文件
(4)容器级的国际化
Application实现了MessageSource接口,要配置文件中的Bean的名称必须为"messageSource".
Spring xml配置 bean.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" 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-3.0.xsd"> <bean id="myResource1" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> <bean id="myResource2" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> <property name="cacheSeconds" value="2"/> </bean> <bean id="messageSource12" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> <!-- 容器级国际化,名称只能为messageSource --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> </beans>
Java code:
package com.baobaotao.i18n; import java.text.MessageFormat; import java.util.GregorianCalendar; import java.util.Locale; import org.springframework.context.ApplicationContext; import org.springframework.context.MessageSource; import org.springframework.context.support.ClassPathXmlApplicationContext; public class I18nGreeting { private static void rsrBdlMessageResource(){ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); MessageSource ms = (MessageSource)ctx.getBean("myResource1"); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = ms.getMessage("greeting.common",params,Locale.US); String str2 = ms.getMessage("greeting.morning",params,Locale.CHINA); String str3 = ms.getMessage("greeting.afternoon",params,Locale.CHINA); System.out.println(str1); System.out.println(str2); System.out.println(str3); } private static void rrsrBdlMessageResource() throws Exception{ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); MessageSource ms = (MessageSource)ctx.getBean("myResource2"); Object[] params = {"John", new GregorianCalendar().getTime()}; for (int i = 0; i < 2; i++) { String str1 = ms.getMessage("greeting.common",params,Locale.US); System.out.println(str1); Thread.currentThread().sleep(20000); } } private static void ctxMessageResource() throws Exception{ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = ctx.getMessage("greeting.common",params,Locale.US); String str2 = ctx.getMessage("greeting.morning",params,Locale.CHINA); System.out.println(str1); System.out.println(str2); } public static void main(String[] args) throws Exception{ // rsrBdlMessageResource(); // rrsrBdlMessageResource(); ctxMessageResource(); } }