1:常用的一些国际化使用类
1.1: java.util.Locale类
构造方法:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
几个使用的范例
//带有语言和国家/地区信息的本地化对象
Locale locale=new Locale("zh","CN");
//只有语言信息的本地化对象
Locale locale2=new Locale("zh");
//等同于Locale("zh","CN")
Locale locale3=Locale.CHINA;
//等同于Locale("zh")
Locale locale4=Locale.CHINESE;
//获取本地系统默认的本地化对象
Locale locale5=Locale.getDefault();
1.2: java.text.NumberFormat
继承关系:
|- java.lang.Object
java.text.Format
java.text.NumberFormat
使用范例:
NumberFormat currFmt=NumberFormat.getCurrencyInstance(Lnew Locale("zh","CN"));
double amt=123.456;
System.out.println(currFmt.format(amt));//直接输出结果为 ¥123.46
1.3: java.text.DateFormat
继承关系:
|- java.lang.Object
java.text.Format
java.text.DateFormat
Locale locale6=new Locale("en","US");
Date date=new Date();
DateFormat df=DateFormat.getDateInstance(DateFormat.MEDIUM, locale6);
System.out.println(df.format(date));//输出结果为: Sep 13, 2015
1.4 java.text.MessageFormat
同样继承java.text.Foramt。功能比较多。下面写一个范例,还有一个别人(^_^肥仔John )总结的内容连接:
String pattern1="{0},你好!你于{1}在银行存入{2}元";
String pattern2="At{1,time,long} On {1,date,long}, {0} paid {2,number,currency}.";
Object[] params={"John",new GregorianCalendar().getTime(),1.0E3};
String msg1=MessageFormat.format(pattern1, params);
MessageFormat mf=new MessageFormat(pattern2,Locale.US);
String msg2=mf.format(params);
System.out.println(msg1);//输出为: John,你好!你于15-9-13 下午2:17在银行存入1,000元
System.out.println(msg2);//输出为: At2:17:29 PM CST On September 13, 2015, John paid $1,000.00.
其他人整理的:初探MessageFormat.format和ChoiceFormat
2:将国际化内容写入资源文件并使用
2.1:资源文件命名规则
国际化资源文件的命名规范规定资源名称采用以下的方式进行命名:
<资源名>_<语言代码>_<国家/地区代码>.properties
其中,语言代码和国家/地区代码都是可选的。<资源名>.properties命名的国际化资源文件是默认的资源文件,即某个本地化类型在系统中找不到对应的资源文件,就采用这个默认的资源文件。<资源名>_<语言代码>.properties 命名的国际化资源文件是某一语言默认的资源文件,即某个本地化类型在系统中找不到精确匹配的资源文件,将采用想应语言迷人的资源文件。
下面建立资源为:resource_zh_CN.properties :(中文编码转换为Unicode码。在Eclipse中自动转换)
greeting.common=\u4F60\u597D\!
greeting.morning=\u65E9\u4E0A\u597D\!
greeting.afternoon=\u4E2D\u5348\u597D\!
resource_en_US.properties:
greeting.common=How are you!
greeting.morning=Good morning!
greeting.afternoon=Good Afternoon\!
2.2: 使用java.util.ResourceBundle读取资源文件
ResourceBundle rb1=ResourceBundle.getBundle("com/aitiny/I18n/resource",Locale.US); //资源名包到名字即可
System.out.println("us:"+rb1.getString("greeting.common")); //输出:us:How are you!
ResourceBundle rb2=ResourceBundle.getBundle("com/aitiny/I18n/resource",Locale.CHINA);
System.out.println("zh:"+rb2.getString("greeting.common")); //输出: zh:你好!
在使用资源文件时候可以使用MessageFormat对资源文件中出现的占位符进行替换。对资源文件进行修改,如:
greeting.common=How are you!{0},today is {1}
读取相同,使用的时候替换如下:
Object [] params={"John",new GregorianCalendar().getTime()};
String str1=new MessageFormat(rb1.getString("greeting.common"),Locale.US).format(params);
System.out.println(str1); //输出结果为: How are you!John, today is 13/9/2015 2:40 PM
3: Spring 使用国际化文件
Spring 定义了访问国际化信息的 org.springframework.context.MessageSource 接口,并提供了几个易用的实现类。首先来了解下该接口的几个重要方法:String getMessage(String code,Object[] args,Locale locale)throws NoSuchMessageException
String getMessage(String code,Object[] args, String defaultMessage,Locale locale)
String getMessage(MessageSourceResolvable resolvable,Locale locale)throws NoSuchMessageException // 封装属性名,参数数组以及默认信息
3.1使用MessageSource 实现类ResourceBundleMessageSource,ReloadableResourceBundleMessageSource来读取资源文件
配置bean:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com/aitiny/I18n/resource</value>
</list>
</property>
</bean>
读取配置信息获取bean,读取资源文件:
String[] configs={"com/aitiny/I18n/spring/beans.xml"};
ApplicationContext ctx=new ClassPathXmlApplicationContext(configs);
MessageSource ms=(MessageSource) ctx.getBean("messageSource");
System.out.println(ms.getMessage("greeting.common", null, Locale.US)); //输出:How are you!
System.out.println(ms.getMessage("greeting.common", null, Locale.CHINA)); //输出:你好!
ReloadableResourceBundleMessageSource类多啦一个自动刷新读取资源文件能力。在bean中配置时配置:
<property name="cacheSeconds" value="5"/> <!-- 这个是以秒为单位的-->
5s自动刷新一次,一般建议小于30分钟。cacheSeconds默认值为 -1 表示永不刷新,此时等于和ResourceBundleMessageSource一样
3.2 容器级的国际化资源
因为 ApplicationContext类继承MessageSource接口,所以同样可以使用其获取资源文件(刚开始学习,掌握使用,内部实现和意义以后讨论)。首先配置bean和3.1中的相同。使用如下,同样直接输出观察
String[] configs={"com/aitiny/I18n/spring/beans.xml"};
ApplicationContext ctx=new ClassPathXmlApplicationContext(configs);
System.out.println(ctx.getMessage("greeting.common", null, Locale.CHINA)); //输出结果: 你好!