MessageFormat:如果一段字符串中有多个对地区敏感的信息,可以使用此类进行批量国际化设置
(1)MessageFormat类允许开发人员用占位符{0}{1}{2}…替换掉字符串中的敏感数据(即国际化相关的数据)。
(2)MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
(3)占位符有三种方式书写方式:
{argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号
{argumentIndex,formatType}: 参数的格式化类型
{argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。
formatType:
number
date
time
choice
fomatStyle
short
medium
long
full
integer
currency
percent
subformatpattern
(4)例子
//模式字符串
//At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
@Test
public void demo6(){
String str = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";
Calendar calendar = Calendar.getInstance();
calendar.set(1998, 6, 3, 12, 30, 0);
Date date = calendar.getTime();
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, Locale.CHINA);
String dateStr = format.format(date);
Object [] objs = {dateStr,99,"$1000000"};
String result = MessageFormat.format(str,objs);
System.out.println(result);
}
@Test
public void test(){
//At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
String msg = "At {0,date,FULL}, a hurricance destroyed {1,number} houses and caused {2,number,currency} of damage";
MessageFormat format = new MessageFormat(msg,Locale.CHINA);
Calendar c = Calendar.getInstance();
c.set(1998, 6, 3);
Date date = c.getTime();
Object [] objs = {date,99,1000000};
String str = format.format(objs);
System.out.println(str);
}
(1)MessageFormat类允许开发人员用占位符{0}{1}{2}…替换掉字符串中的敏感数据(即国际化相关的数据)。
(2)MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
(3)占位符有三种方式书写方式:
{argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号
{argumentIndex,formatType}: 参数的格式化类型
{argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。
formatType:
number
date
time
choice
fomatStyle
short
medium
long
full
integer
currency
percent
subformatpattern
(4)例子
//模式字符串
//At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
@Test
public void demo6(){
String str = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";
Calendar calendar = Calendar.getInstance();
calendar.set(1998, 6, 3, 12, 30, 0);
Date date = calendar.getTime();
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, Locale.CHINA);
String dateStr = format.format(date);
Object [] objs = {dateStr,99,"$1000000"};
String result = MessageFormat.format(str,objs);
System.out.println(result);
}
@Test
public void test(){
//At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
String msg = "At {0,date,FULL}, a hurricance destroyed {1,number} houses and caused {2,number,currency} of damage";
MessageFormat format = new MessageFormat(msg,Locale.CHINA);
Calendar c = Calendar.getInstance();
c.set(1998, 6, 3);
Date date = c.getTime();
Object [] objs = {date,99,1000000};
String str = format.format(objs);
System.out.println(str);
}