- java.lang.Object
-
- java.text.Format
-
- java.text.MessageFormat
-
- 实现的所有接口
-
Serializable,Cloneable
public class MessageFormat extends Format
MessageFormat提供了一种以与语言MessageFormat的方式生成连接消息的方法。 使用此选项可构建为最终用户显示的消息。MessageFormat获取一组对象,对其进行格式化,然后将格式化的字符串插入到适当位置的模式中。注意:
MessageFormat与其他Format类的不同之处在于,您使用其构造函数(不使用getInstance样式工厂方法)创建MessageFormat对象。 工厂方法不是必需的,因为MessageFormat本身不实现特定于语言环境的行为。 任何特定于语言环境的行为都由您提供的模式以及用于插入参数的子格式定义。Patterns and Their Interpretation
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在String中 ,可以使用一对单引号来引用除单引号之外的任何任意字符。 例如,模式字符串
"'{0}'"表示字符串"{0}",而不是FormatElement 。 单引号本身必须在整个String''双引号''表示。 例如,模式字符串"'{''}'"被解释为序列'{(引用开始和左大括号),''(单引号)和}'(右大括号和引用结束), 而不是'{'和'}'(引用左右花括号):表示字符串"{'}", 而不是"{}"。SubformatPattern由其对应的子格式解释,并且依赖于子格式的模式规则适用。 例如,模式字符串
"{1,number,$'#',##}"(带下划线的SubformatPattern )将生成带引号的数字格式,其结果如下:"$#31,45"。 有关详细信息,请参阅每个Format子类文档。在给定模式的末尾,任何不匹配的引用都被视为已关闭。 例如,模式字符串
"'{0}"被视为模式"'{0}'"。必须平衡无引号模式中的任何花括号。 例如,
"ab {0} de"和"ab '}' de"是有效的模式,但"ab {0'}' de","ab } de"和"''{''"都没有。- 警告:
-
遗憾的是,在消息格式模式中使用引号的规则已经显得有些混乱。
特别是,对于本地化者来说,单引号是否需要加倍并不总是显而易见的。
确保通知本地化程序有关规则,并告诉他们(例如,通过使用资源包源文件中的注释)
MessageFormat将处理哪些字符串。 请注意,本地化程序可能需要在原始版本没有的翻译字符串中使用单引号。
ArgumentIndex值是使用数字
'0'到'9'写入的非负整数,表示传递给format方法的arguments数组或format方法返回的结果数组的parse。FormatType和FormatStyle值用于为format元素创建
Shows how FormatType and FormatStyle values map to Format instances FormatType FormatStyle Subformat Created (none) (none)Format实例。 下表显示了值如何映射到Format实例。 表中未显示的组合是非法的。 SubformatPattern必须是所使用的Format子类的有效模式字符串。nullnumber(none)NumberFormat.getInstance(getLocale())integerNumberFormat.getIntegerInstance(getLocale())currencyNumberFormat.getCurrencyInstance(getLocale())percentNumberFormat.getPercentInstance(getLocale())SubformatPatternnewDecimalFormat(subformatPattern,DecimalFormatSymbols.getInstance(getLocale()))date(none)DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())shortDateFormat.getDateInstance(DateFormat.SHORT, getLocale())mediumDateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())longDateFormat.getDateInstance(DateFormat.LONG, getLocale())fullDateFormat.getDateInstance(DateFormat.FULL, getLocale())SubformatPatternnewSimpleDateFormat(subformatPattern, getLocale())time(none)DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())shortDateFormat.getTimeInstance(DateFormat.SHORT, getLocale())mediumDateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())longDateFormat.getTimeInstance(DateFormat.LONG, getLocale())fullDateFormat.getTimeInstance(DateFormat.FULL, getLocale())SubformatPatternnewSimpleDateFormat(subformatPattern, getLocale())choiceSubformatPatternnewChoiceFormat(subformatPattern)使用信息
以下是一些使用示例。 在真正的国际化程序中,消息格式模式和其他静态字符串当然可以从资源包中获得。 其他参数将在运行时动态确定。
第一个示例使用静态方法
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生成的字符串被视为特殊字符串; '{'的出现用于表示子格式,并导致递归。 如果以编程方式创建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")}Synchronization
消息格式不同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部进行同步。
- 从以下版本开始:
- 1.1
- 另请参见:
-
Locale,Format,NumberFormat,DecimalFormat,DecimalFormatSymbols,ChoiceFormat,DateFormat,SimpleDateFormat, Serialized Form
-
-
嵌套类汇总
嵌套类 变量和类型 类 描述 static classMessageFormat.Field定义从MessageFormat.formatToCharacterIterator返回的AttributedCharacterIterator中用作属性键的MessageFormat.formatToCharacterIterator。
-
构造方法摘要
构造方法 构造器 描述 MessageFormat(String pattern)为默认的FORMAT语言环境和指定的模式构造MessageFormat。MessageFormat(String pattern, Locale locale)为指定的语言环境和模式构造MessageFormat。
-
方法摘要
所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 voidapplyPattern(String pattern)设置此消息格式使用的模式。Objectclone()创建并返回此对象的副本。booleanequals(Object obj)两个消息格式对象之间的相等比较StringBufferformat(Object[] arguments, StringBuffer result, FieldPosition pos)格式化一个对象数组,并将MessageFormat的模式(格式化元素替换为格式化对象)StringBuffer到提供的StringBuffer。StringBufferformat(Object arguments, StringBuffer result, FieldPosition pos)格式化一个对象数组,并将MessageFormat的模式(格式化元素替换为格式化对象)StringBuffer到提供的StringBuffer。static Stringformat(String pattern, Object... arguments)使用给定的模式创建MessageFormat并使用它来格式化给定的参数。AttributedCharacterIteratorformatToCharacterIterator(Object arguments)格式化一个对象数组并将它们插入到MessageFormat的模式中,生成AttributedCharacterIterator。Format[]getFormats()获取先前设置的模式字符串中用于格式元素的格式。Format[]getFormatsByArgumentIndex()获取用于传递到format方法或从parse方法返回的值的格式。LocalegetLocale()获取创建或比较子格式时使用的语言环境。inthashCode()生成消息格式对象的哈希码。Object[]parse(String source)从给定字符串的开头解析文本以生成对象数组。Object[]parse(String source, ParsePosition pos)解析字符串。ObjectparseObject(String source, ParsePosition pos)从字符串解析文本以生成对象数组。voidsetFormat(int formatElementIndex, Format newFormat)使用先前设置的模式字符串中具有给定格式元素索引的format元素设置格式。voidsetFormatByArgumentIndex(int argumentIndex, Format newFormat)设置用于先前设置的模式字符串中使用给定参数索引的格式元素的格式。voidsetFormats(Format[] newFormats)设置用于先前设置的模式字符串中的格式元素的格式。voidsetFormatsByArgumentIndex(Format[] newFormats)设置用于传递到format方法或从parse方法返回的值的格式。voidsetLocale(Locale locale)设置创建或比较子格式时要使用的语言环境。StringtoPattern()返回表示消息格式当前状态的模式。-
声明方法的类 java.text.Format
format, parseObject
-
-
-
-
构造方法详细信息
-
MessageFormat
public MessageFormat(String pattern)
为默认的FORMAT语言环境和指定的模式构造MessageFormat。 构造函数首先设置区域设置,然后解析模式并为其中包含的格式元素创建子格式列表。 模式及其解释在class description中指定。- 参数
-
pattern- 此消息格式的模式 - 异常
-
IllegalArgumentException- 如果模式无效 -
NullPointerException- 如果pattern是null
-
MessageFormat
public MessageFormat(String pattern, Locale locale)
- 参数
-
pattern- 此消息格式的模式 -
locale- 此消息格式的区域设置 - 异常
-
IllegalArgumentException- 如果模式无效 -
NullPointerException- 如果pattern是null - 从以下版本开始:
- 1.4
-
-
方法详细信息
-
setLocale
public void setLocale(Locale locale)
设置创建或比较子格式时要使用的语言环境。 这会影响后续调用-
applyPattern和toPattern方法,如果格式元素指定格式类型,因此具有在applyPattern方法中创建的子格式,以及 -
format和formatToCharacterIterator方法如果格式元素未指定格式类型,因此在格式化方法中创建子格式。
- 参数
-
locale- 创建或比较子格式时要使用的语言环境
-
-
getLocale
public Locale getLocale()
获取创建或比较子格式时使用的语言环境。- 结果
- 创建或比较子格式时使用的语言环境
-
applyPattern
public void applyPattern(String pattern)
- 参数
-
pattern- 此消息格式的模式 - 异常
-
IllegalArgumentException- 如果模式无效 -
NullPointerException- 如果pattern是null
-
toPattern
public String toPattern()
返回表示消息格式当前状态的模式。 字符串由内部信息构成,因此不一定等于先前应用的模式。- 结果
- 表示消息格式当前状态的模式
-
setFormatsByArgumentIndex
public void setFormatsByArgumentIndex(Format[] newFormats)
设置用于传递到format方法或从parse方法返回的值的格式。newFormats的元素newFormats对应于先前设置的模式字符串中使用的参数索引。 的格式中的顺序newFormats因此对应于在元素的顺序arguments阵列传递给format方法或由所述返回的结果阵列parse方法。如果参数索引用于模式字符串中的多个格式元素,则相应的新格式将用于所有此类格式元素。 如果参数索引未用于模式字符串中的任何格式元素,则忽略相应的新格式。 如果提供的格式少于所需的格式,则仅替换小于
newFormats.length参数索引的格式。- 参数
-
newFormats- 要使用的新格式 - 异常
-
NullPointerException- 如果newFormats为空 - 从以下版本开始:
- 1.4
-
setFormats
public void setFormats(Format[] newFormats)
设置用于先前设置的模式字符串中的格式元素的格式。newFormats的格式newFormats对应于模式字符串中格式元素的顺序。如果提供的格式多于模式字符串所需的格式,则忽略其余格式。 如果提供的格式少于所需的格式,则只替换第一批
newFormats.length格式。由于模式字符串中格式元素的顺序通常在本地化期间发生变化,因此通常最好使用
setFormatsByArgumentIndex方法,该方法假定格式顺序对应于传递给format方法或结果数组的arguments数组中元素的顺序由parse方法返回。- 参数
-
newFormats- 要使用的新格式 - 异常
-
NullPointerException- 如果newFormats为空
-
setFormatByArgumentIndex
public void setFormatByArgumentIndex(int argumentIndex, Format newFormat)设置用于先前设置的模式字符串中使用给定参数索引的格式元素的格式。 参数索引是格式元素定义的一部分,表示传递给format方法的arguments数组或format方法返回的结果数组的parse。如果参数索引用于模式字符串中的多个格式元素,则新格式将用于所有此类格式元素。 如果参数索引未用于模式字符串中的任何格式元素,则忽略新格式。
- 参数
-
argumentIndex- 要使用新格式的参数索引 -
newFormat- 要使用的新格式 - 从以下版本开始:
- 1.4
-
setFormat
public void setFormat(int formatElementIndex, Format newFormat)使用先前设置的模式字符串中具有给定格式元素索引的format元素设置格式。 format元素索引是从模式字符串的开头开始计算的格式元素的从零开始的数字。由于模式字符串中格式元素的顺序通常在本地化期间发生变化,因此通常最好使用
setFormatByArgumentIndex方法,该方法根据指定的参数索引访问格式元素。- 参数
-
formatElementIndex- 模式中格式元素的索引 -
newFormat- 用于指定格式元素的格式 - 异常
-
ArrayIndexOutOfBoundsException- 如果formatElementIndex等于或大于模式字符串中的格式元素数
-
getFormatsByArgumentIndex
public Format[] getFormatsByArgumentIndex()
获取用于传递到format方法或从parse方法返回的值的格式。 返回数组中的元素索引对应于先前设置的模式字符串中使用的参数索引。 的返回的数组中的格式的顺序因此对应于在元素的顺序arguments阵列传递给format方法或由所述返回的结果阵列parse方法。如果参数索引用于模式字符串中的多个格式元素,则在数组中返回用于最后一个此类格式元素的格式。 如果参数索引未用于模式字符串中的任何格式元素,则在数组中返回null。
- 结果
- 用于模式中参数的格式
- 从以下版本开始:
- 1.4
-
getFormats
public Format[] getFormats()
获取先前设置的模式字符串中用于格式元素的格式。 返回数组中的格式顺序对应于模式字符串中格式元素的顺序。由于模式字符串中格式元素的顺序通常在本地化期间发生变化,因此通常最好使用
getFormatsByArgumentIndex方法,该方法假定格式顺序对应于传递给format方法的arguments数组中的元素顺序或返回的结果数组由parse方法。- 结果
- 用于模式中格式元素的格式
-
format
public final StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
格式化一个对象数组,并将MessageFormat的模式(格式化元素替换为格式化对象)StringBuffer到提供的StringBuffer。替换单个格式元素的文本是从format元素的当前子格式和格式元素的参数索引处的
Examples of subformat,argument,and formatted text Subformat Argument Formatted Text any unavailablearguments元素派生的,如下表的第一个匹配行所示。 如果arguments是null或者少于argumentIndex + 1个元素,则参数不可用 。"{" + argumentIndex + "}"null"null"instanceof ChoiceFormatanysubformat.format(argument).indexOf('{') >= 0 ?
(new MessageFormat(subformat.format(argument), getLocale())).format(argument) : subformat.format(argument)!= nullanysubformat.format(argument)nullinstanceof NumberNumberFormat.getInstance(getLocale()).format(argument)instanceof DateDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)instanceof Stringargumentanyargument.toString()如果
pos为非null,并且引用Field.ARGUMENT,则将返回第一个格式化字符串的位置。- 参数
-
arguments- 要格式化和替换的对象数组。 -
result- 附加文本的位置。 -
pos- 跟踪输出字符串中第一个替换参数的位置。 - 结果
-
传入的字符串缓冲区为
result,附加了格式化文本 - 异常
-
IllegalArgumentException- 如果arguments数组中的参数不是使用它的格式元素所期望的类型。 -
NullPointerException- 如果result是null
-
format
public static String format(String pattern, Object... arguments)
使用给定的模式创建MessageFormat并使用它来格式化给定的参数。 这相当于(newMessageFormat(pattern)).format(arguments, new StringBuffer(), null).toString()- 参数
-
pattern- 模式字符串 -
arguments- 要格式化的对象 - 结果
- 格式化的字符串
- 异常
-
IllegalArgumentException- 如果模式无效,或者arguments数组中的参数不是使用它的格式元素所期望的类型。 -
NullPointerException- 如果pattern是null
-
format
public final StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)
格式化一个对象数组,并将MessageFormat的模式(格式化元素替换为格式化对象)StringBuffer到提供的StringBuffer。 这相当于format((Object[]) arguments, result, pos)- Specified by:
-
format在课程Format - 参数
-
arguments- 要格式化和替换的对象数组。 -
result- 附加文本的位置。 -
pos- 跟踪输出字符串中第一个替换参数的位置。 - 结果
-
传入的字符串缓冲区为
toAppendTo,附加了格式化文本 - 异常
-
IllegalArgumentException- 如果arguments数组中的参数不是使用它的格式元素所期望的类型。 -
NullPointerException- 如果result是null
-
formatToCharacterIterator
public AttributedCharacterIterator formatToCharacterIterator(Object arguments)
格式化一个对象数组并将它们插入到MessageFormat的模式中,生成一个AttributedCharacterIterator。 您可以使用返回的AttributedCharacterIterator来构建生成的String,以及确定有关生成的String的信息。返回的
AttributedCharacterIterator的文本与将返回的文本相同format(arguments, new StringBuffer(), null).toString()此外,
AttributedCharacterIterator至少包含指示从arguments数组中的参数生成文本的位置的属性。 这些属性的键的类型为MessageFormat.Field,它们的值为Integer对象,这些对象指示生成文本的参数的arguments数组中的索引。MessageFormat使用的基础Format实例的属性/值也将放置在生成的AttributedCharacterIterator。 这使您不仅可以在结果字符串中找到参数的位置,还可以查找它依次包含哪些字段。- 重写:
-
formatToCharacterIterator在课程Format - 参数
-
arguments- 要格式化和替换的对象数组。 - 结果
- AttributedCharacterIterator描述格式化的值。
- 异常
-
NullPointerException- 如果arguments为空。 -
IllegalArgumentException- 如果arguments数组中的参数不是使用它的格式元素所期望的类型。 - 从以下版本开始:
- 1.4
-
parse
public Object[] parse(String source, ParsePosition pos)
解析字符串。注意事项:在许多情况下,解析可能会失败。 例如:
- 如果模式中没有出现其中一个参数。
- 如果参数的格式丢失信息,例如选择格式,其中大数字格式为“很多”。
- 尚未处理递归(替换字符串包含{n}个引用。)
- 如果解析的某些部分不明确,则不会总是找到匹配(或正确的匹配)。 例如,如果模式“{1},{2}”与字符串参数{“a,b”,“c”}一起使用,则格式为“a,b,c”。 解析结果时,它将返回{“a”,“b,c”}。
- 如果在字符串中多次解析单个参数,则后一个解析获胜。
- 参数
-
source- 要解析的字符串 -
pos- 解析位置 - 结果
- 一组已解析的对象
- 异常
-
NullPointerException- 如果pos是null则为非nullsource字符串。
-
parse
public Object[] parse(String source) throws ParseException
- 参数
-
source- 应该解析其开头的String。 - 结果
-
从字符串解析的
Object数组。 - 异常
-
ParseException- 如果无法解析指定字符串的开头。
-
parseObject
public Object parseObject(String source, ParsePosition pos)
从字符串解析文本以生成对象数组。该方法尝试从
pos给出的索引开始解析文本。 如果解析成功,则在使用最后一个字符之后将索引pos更新为索引(解析不一定使用直到字符串末尾的所有字符),并返回解析的对象数组。 更新后的pos可用于指示下次调用此方法的起点。 如果发生错误,则不更改索引pos,将错误索引pos设置为发生错误的字符的索引,并返回null。有关消息解析的更多信息,请参见
parse(String, ParsePosition)方法。- Specified by:
-
parseObject在课程Format - 参数
-
source- AString,其中一部分应该被解析。 -
pos- 具有索引和错误索引信息的ParsePosition对象,如上所述。 - 结果
-
从字符串解析的
Object数组。 如果出现错误,则返回null。 - 异常
-
NullPointerException- 如果pos为空。
-
equals
public boolean equals(Object obj)
两个消息格式对象之间的相等比较- 重写:
-
equals在课程Object - 参数
-
obj- 要与之比较的引用对象。 - 结果
-
true如果此对象与obj参数相同; 否则为false。 - 另请参见:
-
Object.hashCode(),HashMap
-
hashCode
public int hashCode()
生成消息格式对象的哈希码。- 重写:
-
hashCode在课程Object - 结果
- 此对象的哈希码值。
- 另请参见:
-
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
-