MessageFormat(动态文本)

来源:互联网 发布:sql统计数量 编辑:程序博客网 时间:2024/06/05 17:56

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

原创粉丝点击