码迷,mamicode.com
首页 > 其他好文 > 详细

Locale的使用

时间:2015-04-20 18:40:18      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:locale

1.通过NumberFormat按本地化的方式对货币金额进行格式化操作,运行实例

Locale locale = new Locale("zh", "CN");

Locale locale = new Locale("en", "US");

NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale);

double amt = 123456.78;

System.out.println(currFmt.format(amt));

2.通过DateFormat#getDateInstance(int style,Locale locale)方法按本地化的方式对日期进行格式化操作

Locale locale = new Locale("zh", "CN");

Locale locale = new Locale("en", "US");

Date date = new Date();

DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

System.out.println(df.format(date));

3.用户既可以同时指定语言和国家/地区参数定义一个本地化对象①,也可以仅通过语言参数定义一个泛本地化对象②。Locale类中通过静态常量定义了一些常用的本地化对象,③和④处就直接通过引用常量返回本地化对象。此外,用户还可以获取系统默认的本地化对象,如⑤所示

//①带有语言和国家/地区信息的本地化对象

Locale locale1 = 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();

4.MessageFormat在NumberFormat和DateFormat的基础上提供了强大的占位符字符串的格式化功能,它支持时间、货币、数字以及对象属性的格式化操作

//①信息格式化串

String pattern1 = "{0},你好!你于{1}在工商银行存入{2} 元。";

String pattern2 = "At {1,time,short} 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);

System.out.println(msg2);

结果:

John,你好!你于07-1-8 下午9:58在工商银行存入1,000元。
At 9:58 PM OnJanuary 8, 2007,John paid $1,000.00.

 

4.i18国际化支持

<资源名>_<语言代码>_<国家/地区代码>.properties

其中,语言代码和国家/地区代码都是可选的。<资源名>.properties命名的国际化资源文件是默认的资源文件,即某个本地化类型在系统中找不到对应的资源文件,就采用这个默认的资源文件。<资源名>_<语言代码>.properties命名的国际化资源文件是某一语言默认的资源文件,即某个本地化类型在系统中找不到精确匹配的资源文件,将采用相应语言默认的资源文件。

举一个例子:假设资源名为resource,则语言为英文,国家为美国,则与其对应的本地化资源文件命名为resource_en_US.properties。

信息在资源文件以属性名/值的方式表示:

greeting.common=How are you!
greeting.morning = Good morning!
greeting.afternoon = Good Afternoon!

对应语言为中文,国家/地区为中国大陆的本地化资源文件则命名为resource_zh_ CN.properties,资源文件内容如下:
greeting.common=\u60a8\u597d\uff01
greeting.morning=\u65e9\u4e0a\u597d\uff01
greeting.afternoon=\u4e0b\u5348\u597d\uff01

本地化不同的同一资源文件,虽然属性值各不相同,但属性名却是相同的,这样应用程序就可以通过Locale对象和属性名精确调用到某个具体的属性值了。

 

读者可能已经注意到,上面中文的本地化资源文件内容采用了特殊的编码表示中文字符,这是因为资源文件对文件内容有严格的要求:只能包含ASCII字符。所以必须将非ASCII字符的内容转换为Unicode代码的表示方式。如上面中文的resource_zh_CN.properties资源文件的三个属性值分别是“您好!”、“早上好!”和“下午好!”三个中文字符串对应的Unicode代码串。

如果在应用开发时,直接采用Unicode代码编辑资源文件是很不方便的,所以,通常我们直接使用正常的方式编写资源文件,在测试或部署时再采用工具进行转换。JDK在bin目录下为我们提供了一个完成此项功能的native2ascii工具,它可以将中文字符的资源文件转换为Unicode代码格式的文件,命令格式如下:

引用

native2ascii [-reverse] [-encoding 编码] [输入文件 [输出文件]]



resource_zh_CN.properties包含中文字符并且以UTF-8进行编码,假设将该资源文件放到d:\目录下,通过下面的命令就可以将其转换为Unicode代码的形式:

引用

D:\>native2ascii -encoding utf-8 d:\resource_zh_CN.properties
d:\resource_zh_CN_1.properties


如果应用程序中拥有大量的本地化资源文件,直接通过传统的File操作资源文件显然太过笨拙。Java为我们提供了用于加载本地化资源文件的方便类java.util.ResourceBoundle。

ResourceBoundle为加载及访问资源文件提供便捷的操作,下面的语句从相对于类路径的目录中加载一个名为resource的本地化资源文件:

Java代码 技术分享 技术分享技术分享

  1. ResourceBundle rb = ResourceBundle.getBundle("com/baobaotao/i18n/resource", locale)  

ResourceBundle rb = ResourceBundle.getBundle("com/baobaotao/i18n/resource", locale)



通过以下的代码即可访问资源文件的属性值:

Java代码 技术分享 技术分享技术分享

  1. rb.getString("greeting.common")  

rb.getString("greeting.common")



来看下面的实例:

代码清单5-16  ResourceBoundle

Java代码 技术分享 技术分享技术分享

  1. ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/resource", Locale.US);   

  2. ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/resource", Locale.CHINA);   

  3. System.out.println("us:"+rb1.getString("greeting.common"));   

  4. System.out.println("cn:"+rb2.getString("greeting.common"));  

ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/resource", Locale.US);
ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/resource", Locale.CHINA);
System.out.println("us:"+rb1.getString("greeting.common"));
System.out.println("cn:"+rb2.getString("greeting.common"));



rb1加载了对应美国英语本地化的resource_en_US.properties资源文件;而rb2加载了对应中国大陆中文的resource_zh_CN.properties资源文件。运行上面的代码,将输出以下信息:

引用

us:How are you!
cn:您好!


加载资源文件时,如果不指定本地化对象,将使用本地系统默认的本地化对象。所以,在中文系统中,ResourceBundle.getBundle("com/baobaotao/i18n/resource")语句也将返回和代码清单5-14中rb2相同的本地化资源。

ResourceBundle在加载资源时,如果指定的本地化资源文件不存在,它按以下顺序尝试加载其他的资源:本地系统默认本地化对象对应的资源→默认的资源。上面的例子中,假设我们使用ResourceBundle.getBundle("com/baobaotao/i18n/resource",Locale.CANADA)加载资源,由于不存在resource_en_CA.properties资源文件,它将尝试加载resource_zh_CN.properties的资源文件,假设resource_zh_CN.properties资源文件也不存在,它将继续尝试加载resource.properties的资源文件,如果这些资源都不存在,将抛出java.util.MissingResourceException异常。

在资源文件中使用格式化串

在上面的资源文件中,属性值都是一般的字符串,它们不能结合运行时的动态参数构造出灵活的信息,而这种需求是很常见的。要解决这个问题很简单,只须使用带占位符的格式化串作为资源文件的属性值并结合使用MessageFormat就可以满足要求了。

上面的例子中,我们仅向用户提供一般性问候,下面我们对资源文件进行改造,通过格式化串让问候语更具个性化:

引用

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}



将该资源文件保存在fmt_resource_en_US.properties中,按照同样的方式编写对应的中文本地化资源文件fmt_resource_zh_CN.properties。

下面,我们联合使用ResourceBoundle和MessageFormat得到美国英文的本地化问候语:

代码清单5-17  资源文件格式化串处理

Java代码 技术分享 技术分享技术分享

  1.  //①加载本地化资源   

  2. ResourceBundle rb1 =    

  3.              ResourceBundle.getBundle("com/baobaotao/i18n/fmt_ resource",Locale.US);    

  4. ResourceBundle rb2 =    

  5.               ResourceBundle.getBundle("com/baobaotao/i18n/fmt_ resource",Locale.CHINA);   

  6. Object[] params = {"John"new GregorianCalendar().getTime()};   

  7.   

  8.   

  9. String str1 = new MessageFormat(rb1.getString("greeting.common"),Locale. US).format(params); ②   

  10. String str2 =new MessageFormat(rb2.getString("greeting.morning"),Locale. CHINA).format(params);   

  11. String str3 =new MessageFormat(rb2.getString("greeting.afternoon"),Locale. CHINA).format(params);   

  12. System.out.println(str1);   

  13. System.out.println(str2);   

  14. System.out.println(str3);  

 //①加载本地化资源
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);



运行以上的代码,将输出以下信息:

引用

How are you!John,today is 1/9/07 4:11 PM
早上好!John,现在是下午4:11
下午好!John,现在是2007年1月9日



MessageSource

Spring定义了访问国际化信息的MessageSource接口,并提供了几个易用的实现类。首先来了解一下该接口的几个重要方法:

  • String getMessage(String code, Object[] args, String defaultMessage, Locale locale)

  • code表示国际化资源中的属性名;args用于传递格式化串占位符所用的运行期参数;当在资源找不到对应属性名时,返回defaultMessage参数所指定的默认信息;locale表示本地化对象;


  • String getMessage(String code, Object[] args, Locale locale)

  •    throws NoSuchMessageException


  • 与上面的方法类似,只不过在找不到资源中对应的属性名时,直接抛出NoSuchMessageException异常;


  • String getMessage(MessageSourceResolvable resolvable, Locale locale)

  •    throws NoSuchMessageException


  • MessageSourceResolvable 将属性名、参数数组以及默认信息封装起来,它的功能和第一个接口方法相同。





MessageSource的类结构

MessageSource分别被HierarchicalMessageSource和ApplicationContext接口扩展,这里我们主要看一下HierarchicalMessageSource接口的几个实现类,如图5-7所示:

技术分享

HierarchicalMessageSource接口添加了两个方法,建立父子层级的MessageSource结构,类似于前面我们所介绍的HierarchicalBeanFactory。该接口的setParentMessageSource (MessageSource parent)方法用于设置父MessageSource,而getParentMessageSource()方法用于返回父MessageSource。

HierarchicalMessageSource接口最重要的两个实现类是ResourceBundleMessageSource和ReloadableResourceBundleMessageSource。它们基于Java的ResourceBundle基础类实现,允许仅通过资源名加载国际化资源。ReloadableResourceBundleMessageSource提供了定时刷新功能,允许在不重启系统的情况下,更新资源的信息。StaticMessageSource主要用于程序测试,它允许通过编程的方式提供国际化信息。而DelegatingMessageSource是为方便操作父MessageSource而提供的代理类。

ResourceBundleMessageSource

该实现类允许用户通过beanName指定一个资源名(包括类路径的全限定资源名),或通过beanNames指定一组资源名。在代码清单5-18中,我们通过JDK的基础类完成了本地化的操作,下面我们使用ResourceBundleMessageSource来完成相同的任务。读者可以比较两者的使用差别,并体会Spring所提供的国际化处理功能所带给我们的好处:

代码清单5-18  通过ResourceBundleMessageSource配置资源

Xml代码 技术分享 技术分享技术分享

  1. <bean id="myResource"  

  2. class="org.springframework.context.support.ResourceBundleMessageSource">  

  3.     <!--①通过基名指定资源,相对于类根路径-->  

  4.     <property name="basenames">     

  5.        <list>  

  6.           <value>com/baobaotao/i18n/fmt_resource</value>  

  7.        </list>  

  8.     </property>  

  9.   </bean>   

<bean id="myResource"
class="org.springframework.context.support.ResourceBundleMessageSource">
    <!--①通过基名指定资源,相对于类根路径-->
    <property name="basenames">  
       <list>
          <value>com/baobaotao/i18n/fmt_resource</value>
       </list>
    </property>
  </bean>



启动Spring容器,并通过MessageSource访问配置的国际化资源,如代码清单 5 19所示:

代码清单5-19  访问国际化消息:ResourceBundleMessageSource

Java代码 技术分享 技术分享技术分享

  1. String[] configs = {"com/baobaotao/i18n/beans.xml"};   

  2. ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);   

  3.   

  4. //①获取MessageSource的Bean   

  5. MessageSource ms = (MessageSource)ctx.getBean("myResource");    

  6. Object[] params = {"John"new GregorianCalendar(). getTime()};   

  7.   

  8. //②获取格式化的国际化信息   

  9. String str1 = ms.getMessage("greeting.common",params,Locale.US);   

  10. String str2 = ms.getMessage("greeting.morning",params,Locale.CHINA);   

  11. String str3 = ms.getMessage("greeting.afternoon",params,Locale.CHINA);   

  12. System.out.println(str1);   

  13. System.out.println(str2);   

  14. System.out.println(str3);  

String[] configs = {"com/baobaotao/i18n/beans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);

//①获取MessageSource的Bean
MessageSource ms = (MessageSource)ctx.getBean("myResource"); 
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);



比较代码清单5-19中的代码,我们发现最主要的区别在于我们无须再分别加载不同语言、不同国家/地区的本地化资源文件,仅仅通过资源名就可以加载整套的国际化资源文件。此外,我们无须显式使用MessageFormat操作国际化信息,仅通过MessageSource# getMessage()方法就可以完成操作了。这段代码的运行结果与代码清单5 17的运行结果完全一样。

ReloadableResourceBundleMessageSource

前面,我们提到该实现类比之于ResourceBundleMessageSource的唯一区别在于它可以定时刷新资源文件,以便在应用程序不重启的情况下感知资源文件的变化。很多生产系统都需要长时间持续运行,系统重启会给运行带来很大的负面影响。这时,通过该实现类就可以解决国际化信息更新的问题。请看下面的配置:

代码清单5-20  通过ReloadableResourceBundleMessageSource配置资源

Xml代码 技术分享 技术分享技术分享

  1. <bean id="myResource"    

  2. lass="org.springframework.context.support. ReloadableResourceBundleMessageSource">  

  3.    <property name="basenames">  

  4.       <list>  

  5.         <value>com/baobaotao/i18n/fmt_resource</value>  

  6.       </list>  

  7.    </property>  

  8.    <!--① 刷新资源文件的周期,以秒为单位-->  

  9.    <property name="cacheSeconds" value="5"/>    

  10.  </bean>  

 <bean id="myResource" 
class="org.springframework.context.support. ReloadableResourceBundleMessageSource">
    <property name="basenames">
       <list>
         <value>com/baobaotao/i18n/fmt_resource</value>
       </list>
    </property>
    <!--① 刷新资源文件的周期,以秒为单位-->
    <property name="cacheSeconds" value="5"/> 
  </bean>



在上面的配置中,我们通过cacheSeconds属性让ReloadableResourceBundleMessageSource每5秒钟刷新一次资源文件(在真实的应用中,刷新周期不能太短,否则频繁的刷新将带来性能上的负面影响,一般不建议小于30分钟)。cacheSeconds默认值为-1表示永不刷新,此时,该实现类的功能就蜕化为ResourceBundleMessageSource的功能。

我们编写一个测试类对上面配置的ReloadableResourceBundleMessageSource进行测试:

代码清单5-21  刷新资源:ReloadableResourceBundleMessageSource

Java代码 技术分享 技术分享技术分享

  1. String[] configs = {"com/baobaotao/i18n/beans.xml"};   

  2. ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);   

  3.   

  4. MessageSource ms = (MessageSource)ctx.getBean("myResource");   

  5. Object[] params = {"John"new GregorianCalendar().getTime()};   

  6.   

  7. for (int i = 0; i < 2; i++) {   

  8.     String str1 = ms.getMessage("greeting.common",params,Locale.US);       

  9.     System.out.println(str1);   

  10.     Thread.currentThread().sleep(20000); //①模拟程序应用,在此期间,我们更改资源文件    

  11. }  

String[] configs = {"com/baobaotao/i18n/beans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);

MessageSource ms = (MessageSource)ctx.getBean("myResource");
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); //①模拟程序应用,在此期间,我们更改资源文件 
}



在①处,我们让程序睡眠20秒钟,在这期间,我们将fmt_resource_zh_CN.properties资源文件的greeting.common键值调整为:

引用

---How are you!{0},today is {1}---



我们将看到两次输出的格式化信息分别对应更改前后的内容,也即本地化资源文件的调整被自动生效了:

引用

How are you!John,today is 1/9/07 4:55 PM
---How are you!John,today is 1/9/07 4:55 PM---



容器级的国际化信息资源

在如图5-7所示的MessageSource类图结构中,我们发现ApplicationContext实现了MessageSource的接口。也就是说ApplicationContext的实现类本身也是一个MessageSource对象。

将ApplicationContext和MessageSource整合起来,乍一看挺让人费解的,Spring这样设计的意图究竟是什么呢?原来Spring认为:在一般情况下,国际化信息资源应该是容器级。我们一般不会将MessageSource作为一个Bean注入到其他的Bean中,相反MessageSource作为容器的基础设施向容器中所有的Bean开放。只要我们考察一下国际化信息的实际消费场所就更能理解Spring这一设计的用意了。国际化信息一般在系统输出信息时使用,如Spring MVC的页面标签,控制器Controller等,不同的模块都可能通过这些组件访问国际化信息,因此Spring就将国际化消息作为容器的公共基础设施对所有组件开放。

既然一般情况下我们不会直接通过引用MessageSource Bean使用国际信息,那如何声明容器级的国际化信息呢?我们其实在5.1.1节讲解Spring容器的内部工作机制时已经埋下了伏笔:在介绍容器启动过程时,我们通过代码清单5-1对Spring容器启动时的步骤进行剖析,④处的initMessageSource()方法所执行的工作就是初始化容器中的国际化信息资源:它根据反射机制从BeanDefinitionRegistry中找出名称为“messageSource”且类型为org.springframework.context.MessageSource的Bean,将这个Bean定义的信息资源加载为容器级的国际化信息资源。请看下面的配置:

代码清单5-22  容器级资源的配置

Xml代码 技术分享 技术分享技术分享

  1. <!--①注册资源Bean,其Bean名称只能为messageSource -->  

  2. <bean id="messageSource"    

  3.       class="org.springframework.context.support.ResourceBundleMessageSource">  

  4.   <property name="basenames">  

  5.      <list>  

  6.        <value>com/baobaotao/i18n/fmt_resource</value>  

  7.      </list>  

  8.   </property>  

  9. </bean>  

<!--①注册资源Bean,其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>



下面,我们通过ApplicationContext直接访问国际化信息,如代码清单5 23所示:

代码清单5-23  通过ApplicationContext访问国际化信息

Java代码 技术分享 技术分享技术分享

  1. String[] configs = {"com/baobaotao/i18n/beans.xml"};   

  2. ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);   

  3. //①直接通过容器访问国际化信息   

  4. Object[] params = {"John"new GregorianCalendar().getTime()};   

  5.     

  6. String str1 = ctx.getMessage("greeting.common",params,Locale.US);   

  7. String str2 = ctx.getMessage("greeting.morning",params,Locale.CHINA);      

  8. System.out.println(str1);   

  9. System.out.println(str2);  

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);



运行以上代码,输出以下信息:

引用

How are you!John,today is 1/9/07 5:24 PM
早上好!John,现在是下午5:24



假设MessageSource Bean名字没有命名为“messageSource”,以上代码将抛出NoSuchMessageException异常。

Locale的使用

标签:locale

原文地址:http://5618698.blog.51cto.com/5608698/1636217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!