public class MessageFormat extends Format
MessageFormat提供了一种方法,在一个中性的语言方式产生级联邮件。使用此来构建显示为最终用户的消息。
MessageFormat以一组对象,格式,然后插入已格式化的字符串在适当的地方模式。
注: MessageFormat不同于其他Format类在创建它的一MessageFormat对象的构造函数(不带getInstance风格的工厂方法)。工厂方法并不需要,因为MessageFormat本身并不实现特定场所的行为。任何区域设置特定的行为是由你提供以及用于插入参数的subformats模式定义。
MessageFormat采用以下形式的模式:
MessageFormatPattern:
String
MessageFormatPattern FormatElement String
FormatElement:
{ ArgumentIndex }
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
FormatType: one of
number date time choice
FormatStyle:
short
medium
long
full
integer
currency
percent
SubformatPattern
在字符串,一对单引号可以用单引号引用除了任意字符。例如,模式串"'{0}'"表示字符串"{0}",不是formatelement。单引号本身必须用单引号''一整个字符串代表。例如,模式串"'{''}'"被解释为一个序列的'{(引用和左大括号开始),''(单引号),和}'(右大括号结束引用),不'{'和'}'(引用的左、右大括号):代表字符串"{'}",不"{}"。
一个subformatpattern是由其对应的子格式解释,和子格式依赖模式的规则。例如,模式串"{1,number,$'#',##}"(subformatpattern下划线)会产生与英镑符号引用的数字格式,如:"$#31,45"与结果。指每Format类文件的详细信息。
任何不匹配的报价都被视为封闭在给定的模式结束。例如,模式串"'{0}"作为模式"'{0}'"。
在一个无任何大括号必须平衡模式。例如,"ab {0} de"和"ab '}' de"是有效的模式,但"ab {0'}' de","ab } de"和"''{''"不。
MessageFormat。注意方位词可能需要翻译的字符串在原版本,不要用单引号。
的argumentindex值是一个非负整数用数字'0'通过'9',并传递给format方法或结果数组的parse方法返回的数组索引代表arguments。
的formattype和formatstyle值来创建一个元素的格式Format实例。下表显示的值映射到Format实例。表中未显示的组合是非法的。一个subformatpattern必须为使用Format类有效的模式字符串。
这里有一些使用的例子。在真正的国际化程序中,消息格式模式和其他静态字符串将是从资源包中获得的。其他参数将在运行时动态地确定。
第一个例子使用静态方法MessageFormat.format,其内部创建一个一次性使用的MessageFormat:
int planet = 7;
String event = "a disturbance in the Force";
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
planet, new Date(), event);
输出:
At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
下面的示例创建一个MessageFormat实例,可反复使用:
int fileCount = 1273;
String diskName = "MyDisk";
Object[] testArgs = {new Long(fileCount), diskName};
MessageFormat form = new MessageFormat(
"The disk \"{1}\" contains {0} file(s).");
System.out.println(form.format(testArgs));
与
fileCount不同值的输出:
The disk "MyDisk" contains 0 file(s). The disk "MyDisk" contains 1 file(s). The disk "MyDisk" contains 1,273 file(s).
对于更复杂的模式,你可以使用一个ChoiceFormat产生单数和复数的正确形式:
MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
double[] filelimits = {0,1,2};
String[] filepart = {"no files","one file","{0,number} files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
form.setFormatByArgumentIndex(0, fileform);
int fileCount = 1273;
String diskName = "MyDisk";
Object[] testArgs = {new Long(fileCount), diskName};
System.out.println(form.format(testArgs));
与
fileCount不同值的输出:
The disk "MyDisk" contains no files. The disk "MyDisk" contains one file. The disk "MyDisk" contains 1,273 files.
你可以创建ChoiceFormat编程,在上面的例子中,或通过使用模式。更多信息见ChoiceFormat。
form.applyPattern( "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
注:正如我们上面看到的,在一个ChoiceFormat MessageFormat产生的字符串被视为特殊的;出现“{”用来表示subformats,并引起递归。如果你创建一个MessageFormat和ChoiceFormat编程方式(而不是使用字符串模式),然后不小心产生一种递归本身的格式,这将导致无限循环。
当一个单一的参数在字符串中不止一次地被解析时,最后一个匹配将是解析的最终结果。例如,
MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
Object[] objs = {new Double(3.1415)};
String result = mf.format( objs );
// result now equals "3.14, 3.1"
objs = null;
objs = mf.parse(result, new ParsePosition(0));
// objs now equals {new Double(3.1)}
同样,解析与使用含有相同参数的多次出现的模式MessageFormat对象将返回的最后一场比赛。例如,
MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
String forParsing = "x, y, z";
Object[] objs = mf.parse(forParsing, new ParsePosition(0));
// result now equals {new String("z")}
消息格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一个格式,则必须在外部同步。
| Modifier and Type | Class and Description |
|---|---|
static class |
MessageFormat.Field
定义常数,作为在
AttributedCharacterIterator属性键返回
MessageFormat.formatToCharacterIterator。
|
| Constructor and Description |
|---|
MessageFormat(String pattern)
构建了一个默认的
FORMAT现场和指定的模式格式。
|
MessageFormat(String pattern, Locale locale)
构建了一个指定的区域设置和图形格式。
|
| Modifier and Type | Method and Description |
|---|---|
void |
applyPattern(String pattern)
设置此消息格式使用的模式。
|
Object |
clone()
创建并返回此对象的副本。
|
boolean |
equals(Object obj)
两个消息格式对象之间的等式比较
|
StringBuffer |
format(Object[] arguments, StringBuffer result, FieldPosition pos)
格式对象数组和附加的
MessageFormat的模式,具有格式元素的格式化对象所取代,所提供的
StringBuffer。
|
StringBuffer |
format(Object arguments, StringBuffer result, FieldPosition pos)
格式对象数组和附加的
MessageFormat的模式,具有格式元素的格式化对象所取代,所提供的
StringBuffer。
|
static String |
format(String pattern, Object... arguments)
创建一个与给定的模式和格式用于给定的参数格式。
|
AttributedCharacterIterator |
formatToCharacterIterator(Object arguments)
格式对象并将其插入到
MessageFormat阵列的模式,产生一个
AttributedCharacterIterator。
|
Format[] |
getFormats()
获取先前设置的模式字符串中的格式元素的格式。
|
Format[] |
getFormatsByArgumentIndex()
获取用于值传递到
format方法或从
parse方法返回的格式。
|
Locale |
getLocale()
获取区域的创建或使用时比较subformats。
|
int |
hashCode()
为消息格式对象生成一个哈希代码。
|
Object[] |
parse(String source)
解析文本从给定的字符串生成一个对象数组的开始。
|
Object[] |
parse(String source, ParsePosition pos)
将字符串。
|
Object |
parseObject(String source, ParsePosition pos)
解析文本字符串生成一个对象数组。
|
void |
setFormat(int formatElementIndex, Format newFormat)
设置先前设置的模式字符串中给定的格式元素索引的格式元素的格式。
|
void |
setFormatByArgumentIndex(int argumentIndex, Format newFormat)
设置用于在使用给定参数索引的先前设置的模式字符串中的格式元素的格式。
|
void |
setFormats(Format[] newFormats)
设置先前设置的模式字符串中的格式元素的格式。
|
void |
setFormatsByArgumentIndex(Format[] newFormats)
设置格式使用值传递到
format方法或从
parse方法返回。
|
void |
setLocale(Locale locale)
设置要创建或比较subformats时使用的现场。
|
String |
toPattern()
返回表示消息格式的当前状态的模式。
|
format, parseObjectpublic MessageFormat(String pattern)
FORMAT现场和指定的模式格式。构造函数的第一集的现场,然后解析模式和创建一个列表中的元素subformats格式中。模式及其解释在
class description指定。
pattern -这个消息格式的模式
IllegalArgumentException -如果模式是无效的
public MessageFormat(String pattern, Locale locale)
pattern -这个消息格式的模式
locale -这个消息格式设置
IllegalArgumentException -如果模式是无效的
public void setLocale(Locale locale)
applyPattern和toPattern方法如果格式元素指定格式类型,因此有subformats在applyPattern方法创建的,以及format和formatToCharacterIterator方法如果格式元素不指定格式类型,因此有subformats在格式化的方法创建。locale -现场被创建或使用时比较subformats
public Locale getLocale()
public void applyPattern(String pattern)
pattern -这个消息格式的模式
IllegalArgumentException -如果模式是无效的
public String toPattern()
public void setFormatsByArgumentIndex(Format[] newFormats)
format方法或从
parse方法返回。元素在
newFormats指标对应用于预先设定的模式字符串参数指标。在
newFormats格式的顺序就相当于给
format方法或结果数组的
parse方法返回的数组元素的顺序
arguments。
如果一个参数索引用于模式字符串中的一个以上的格式元素,那么对应的新格式将用于所有这些格式元素。如果在模式字符串中的任何格式元素不使用参数索引,则将忽略相应的新格式。如果少了格式提供了比所需要的,那么只有论证指标小于newFormats.length格式所取代。
newFormats -使用新的格式
NullPointerException -如果
newFormats是空的
public void setFormats(Format[] newFormats)
newFormats格式顺序对应的模式字符串中的格式元素的顺序。
如果提供了比模式字符串所需的更多格式,则忽略了其余的格式。如果少了格式提供比需要的,那么只有一newFormats.length格式所取代。
由于在模式串中经常改变定位格式元素的顺序,最好的方法是使用setFormatsByArgumentIndex方法,即假设对应于传递给format方法或结果数组的parse方法返回的数组元素的顺序arguments格式命令。
newFormats -使用新的格式
NullPointerException -如果
newFormats是空的
public void setFormatByArgumentIndex(int argumentIndex,
Format newFormat)
format方法或结果数组的
parse方法返回的数组索引代表
arguments。
如果在模式字符串中使用多个格式元素的参数索引,那么新的格式将用于所有这些格式元素。如果在模式字符串中的任何格式元素中不使用参数索引,则将忽略新格式的参数。
argumentIndex -它使用新格式的参数指标
newFormat -使用新的格式
public void setFormat(int formatElementIndex,
Format newFormat)
由于在模式串中经常改变定位格式元素的顺序,最好的方法是使用setFormatByArgumentIndex访问方法,基于参数指标他们指定的格式元素。
formatElementIndex在图案的格式元素的索引
newFormat -格式使用指定的格式元素
ArrayIndexOutOfBoundsException -如果
formatElementIndex等于或大于在模式字符串格式的元素个数
public Format[] getFormatsByArgumentIndex()
format方法或从
parse方法返回的格式。返回数组中的元素的索引对应于先前设置的模式字符串中使用的参数索引。在返回的数组中相应于过去的
format方法或结果数组的
parse方法返回的数组元素的顺序
arguments格式的命令。
如果在模式字符串中使用多个格式元素的参数索引,则在数组中返回最后一个这样的格式元素所使用的格式。如果在模式字符串中的任何格式元素不使用参数索引,则在数组中返回空值。
public Format[] getFormats()
由于在模式串中经常改变定位格式元素的顺序,这是通常最好使用getFormatsByArgumentIndex方法,即假设对应于传递给format方法或结果数组的parse方法返回的数组元素的顺序arguments格式命令。
public final StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
MessageFormat的模式,具有格式元素的格式化对象所取代,所提供的
StringBuffer。
文本取代个人的格式元素是来自当前的格式元子格式和arguments元素的格式元素的参数指标由下表的第一行表示匹配。一种说法是不可用的如果arguments是null或少于argumentindex+1元。
| Subformat | Argument | Formatted Text |
|---|---|---|
| any | unavailable | "{" + argumentIndex + "}" |
| any | null |
"null" |
instanceof ChoiceFormat |
any | subformat.format(argument).indexOf('{') >= 0 ? |
!= null |
any | subformat.format(argument) |
null |
instanceof Number |
NumberFormat.getInstance(getLocale()).format(argument) |
null |
instanceof Date |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument) |
null |
instanceof String |
argument |
null |
any | argument.toString() |
如果pos非空,指的是Field.ARGUMENT,第一的位置将返回格式化字符串。
arguments -对象被格式化和取代数组。
result -文本追加。
pos -输入:校准领域,如果需要的话。输出:对准场的偏移量。
result,带格式文本追加
IllegalArgumentException -如果在
arguments数组参数不是预期的类型元素的格式(S)使用它。
public static String format(String pattern, Object... arguments)
(new MessageFormat(pattern)).format(arguments, new StringBuffer(), null).toString()
pattern -模式字符串
arguments对象(S)格式
IllegalArgumentException -如果模式是无效的,或者如果在
arguments数组参数不是预期的类型元素的格式(S)使用它。
public final StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)
MessageFormat的模式,具有格式元素的格式化对象所取代,所提供的
StringBuffer。这相当于
format((Object[]) arguments, result, pos)
format 方法重写,继承类
Format
arguments -对象被格式化和取代数组。
result -文本追加。
pos -输入:校准领域,如果需要的话。输出:对准场的偏移量。
toAppendTo,带格式文本追加
IllegalArgumentException -如果在
arguments数组参数不是预期的类型元素的格式(S)使用它。
public AttributedCharacterIterator formatToCharacterIterator(Object arguments)
MessageFormat阵列的模式,产生一个
AttributedCharacterIterator。你可以使用返回的
AttributedCharacterIterator建立得到的字符串,以及确定得到的字符串信息。
返回的AttributedCharacterIterator文字是相同的,会回来的
format(arguments, new StringBuffer(), null).toString()
此外,该AttributedCharacterIterator至少包含属性指示在文字是从一个在arguments数组参数生成。这些属性的键MessageFormat.Field型,他们的价值观是Integer对象说明指数在arguments数组参数的文本生成。
属性/值从底层Format实例MessageFormat使用也将被放置在所产生的AttributedCharacterIterator。这允许您不仅找到一个参数被放置在所产生的字符串中,而且还发现它包含在哪个字段中。
formatToCharacterIterator 方法重写,继承类
Format
arguments -对象被格式化和取代数组。
NullPointerException -如果
arguments是空的。
IllegalArgumentException -如果在
arguments数组参数不是预期的类型元素的格式(S)使用它。
public Object[] parse(String source, ParsePosition pos)
注意事项:解析可能在一些情况下失败。例如:
source -解析字符串
pos -解析位置
public Object[] parse(String source) throws ParseException
看到消息的更多信息parse(String, ParsePosition)方法解析。
source -
String始应解析。
Object解析字符串数组。
ParseException -如果不能指定字符串的开始被解析。
public Object parseObject(String source, ParsePosition pos)
解析文本开始了pos索引的方法尝试。如果分析成功,那么pos索引更新索引的最后一个字符后(解析不一定使用所有字符到字符串的末尾),并解析对象的数组返回。更新pos可以用来指示下一个调用这个方法的出发点。如果出现错误,那么pos指标没有发生变化,对pos误差指标设置为错误发生位置的字符的索引,并返回null。
看到消息的更多信息parse(String, ParsePosition)方法解析。
parseObject 方法重写,继承类
Format
source -
String,其中一部分会被解析。
pos -一个指标和误差指标上述信息,
ParsePosition对象。
Object解析字符串数组。在错误的情况下,返回空。
NullPointerException -如果
pos是空的。
public boolean equals(Object obj)
equals 方法重写,继承类
Object
obj -参考对象的比较。
true obj参数相同;
false否则。
Object.hashCode(),
HashMap
public int hashCode()
hashCode 方法重写,继承类
Object
Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.