MessageFormat(动态文本)

MessageFormat(动态文本)

1、 如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。

例如:At 12:30 pm on jul 3,1998, ahurricance destroyed 99 houses and caused $1000000 of damage

以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。

2、 MessageFormat 类如何进行批量处理呢?

1)MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。

2)MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。

       模式字符串:  {0}、{1}、{2}占位符

At {0}, a hurricance destroyed {1} houses and caused{2} of damage

String pattern="At {0,time,short} on {0,date,long}, a hurricancedestroyed {1,number} houses and caused{2,number,currency} of damage";

       MessageFormat mf=new MessageFormat(pattern,Locale.US);

       Object []objs={new Date(),new Integer(99),new Double(1e7)};

       String result=mf.format(objs);

        System.out.println(result);

      

       ResourceBundle bundle=ResourceBundle.getBundle("com.hbsi.resource.MyResource",Locale.CHINA);

       result=bundle.getString("title");

       //System.out.println(result);

       MessageFormat mf1=new MessageFormat(result,Locale.CHINA);

       System.out.println(mf1.format(objs));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值