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
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值