`
collegeyuan
  • 浏览: 29589 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

spring国际化

 
阅读更多

ApplicationContext接口继承BeanFactory都是spring的容器。

ApplicationContext接口实现了MessageSource接口,因此具有国际化功能。下面是messageSource接口定义的两个用于国际化的两个方法:

   String getMessage(String code,Object[] args,Locale loc)

   String getMessage(String code,Object[] args,String default,Locale loc);

一、在spring配置文件中加入如下配置:

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basenames">
   <list>
 
<value>message</value>
    </list>
</property>
</bean>
 
二、给出两份资源文件
 
  1)英语资源文件message_en_US.properties
     hello=welcom,{0}
     now=now is :{0}
   
  2)简体中文资源文件message.properties
     hello=欢迎你,{0}
     now=现在时间是 :{0}
    
三、使用native2ascii工具将这份资源文件国际化,命令如下:
    
    native2ascii message.properties message_zh_CN.properties
   
    执行上面命令后,生成message_zh_CN.properties文件,该文件是中文国际化资源文件。
 
四、主程序
 
    public void main(String args[]){
 
      ApplicationContext con = new 
          ClassPathXmlApplicationContext("beans.xml");
    
     String hello =  con.getMessage("hello",new String[]{"随悟空"},
         Locale.getDefault(Locale.Category.Format");
 
 String now=  con.getMessage("now",new String[]{new Date()},
         Locale.getDefault(Locale.Category.Format");
 
  System.out.println(hello);
  System.out.println(now);
 
}
 
程序输出结果会根据环境的不用而输出结果;
 
  在中文环境下结果是:
 
    欢迎你,随悟空
    现在时间是,14-5-10 下午5:43
  
  在英文环境下结果是:
     
     welcom,随悟空
     now is :14/5/10 5:43 PM
     
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics