Most visited

Recently visited

Added in API level 24

PluralFormat

public class PluralFormat
extends UFormat

java.lang.Object
   ↳ java.text.Format
     ↳ android.icu.text.UFormat
       ↳ android.icu.text.PluralFormat


PluralFormat支持创建具有复数变形的国际化消息。 它基于复数选择 ,即呼叫者为每个可能出现在用户语言中的复数情况指定消息,并且PluralFormat根据该号码选择适当的消息。

The Problem of Plural Forms in Internationalized Messages

不同的语言有不同的方式来复用复数。 创建包含复数形式的国际化消息只有在框架能够正确处理所有语言的复数形式时才可行。 ChoiceFormat不能很好地处理这个问题,因为它会为每个消息附加一个数字间隔,并选择其间隔包含给定数字的消息。 这只能处理有限数量的间隔。 但在某些语言中,如波兰语,一个复数情况适用于无限多的时间间隔(例如,除非以12,13或14结尾,否则适用于以2,3或4结尾的数字)。 因此ChoiceFormat是不够的。

PluralFormat通过将问题分为两部分来解决这个问题:

Usage of PluralFormat

注:通常情况下,多格式是通过做 MessageFormatplural参数类型,而不是使用一个独立的 PluralFormat

本讨论假设您使用PluralFormat与一组预定义的复数规则。 您可以使用其中一个采用ULocale对象的构造函数创建一个。 要指定消息模式,可以将其传递给构造函数,也可以使用applyPattern()方法明确设置它。 format()方法采用一个数字对象并选择匹配的复数情况的消息。 此消息将被返回。

Patterns and Their Interpretation

模式文本为指定语言环境的每个复数情况定义了消息输出。 句法:

 pluralStyle = [offsetValue] (selector '{' message '}')+
 offsetValue = "offset:" number
 selector = explicitValue | keyword
 explicitValue = '=' number  // adjacent, no white space in between
 keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
 message: see MessageFormat
 
Pattern_White_Space between syntax elements is ignored, except between the {curly braces} and their sub-message, and between the '=' and the number of an explicitValue.

CLDR / ICU中有6个预定义的病例关键字 - “零”,“一”,“二”,“少”,“多”和“其他”。 您必须为每个规则集中包含的默认复数情况“ other ”定义消息文本。 如果您没有为特定复数情况指定消息文本,则复数情况“ other ”的消息文本被分配给该复数情况。

格式化时,首先将输入号码与explicitValue子句进行匹配。 如果没有精确数字匹配,则通过用输入数字减去偏移量来调用PluralRules来选择关键字。 (如果从模式字符串中省略,偏移量默认为0)如果没有该关键字的子句,则返回“其他”子句。

选定的子消息本身(即嵌套在子消息中的参数之外)中的未加引号的磅符号( # )被输入数字减去偏移量所替代。 数减去偏移值是利用一个格式化NumberFormatPluralFormat的语言环境。 如果您需要特殊数字格式,则必须使用MessageFormat并明确指定NumberFormat参数。 注意:该参数的格式不会减去偏移量! 如果您需要自定义格式并且具有非零偏移量,则需要将数值 - 偏移量值作为单独参数传递。

有关使用示例,请参阅 MessageFormat类文档。

Defining Custom Plural Rules

如果您需要使用PluralFormat自定义规则,则可以创建一个PluralRules对象并将其传递给PluralFormat的构造函数。 如果您还在此构造函数中指定区域设置,则将使用此区域设置格式化消息文本中的数字。

有关 PluralRules更多信息,请参阅 PluralRules

Summary

Public constructors

PluralFormat()

创建一个新的基数号码 PluralFormat默认 FORMAT语言环境。

PluralFormat(ULocale ulocale)

为给定的语言环境创建新的基数 PluralFormat

PluralFormat(Locale locale)

创建一个新的基数号码 PluralFormat对于给定 Locale

PluralFormat(PluralRules rules)

为一组给定的规则创建一个新的基数 PluralFormat

PluralFormat(ULocale ulocale, PluralRules rules)

为给定的一组规则创建新的基数 PluralFormat

PluralFormat(Locale locale, PluralRules rules)

为一组给定的规则创建新的基数 PluralFormat

PluralFormat(ULocale ulocale, PluralRules.PluralType type)

为复数类型创建一个新的 PluralFormat

PluralFormat(Locale locale, PluralRules.PluralType type)

为复数类型创建一个新的 PluralFormat

PluralFormat(String pattern)

为给定的模式字符串创建一个新的基数 PluralFormat

PluralFormat(ULocale ulocale, String pattern)

为给定的模式字符串和语言环境创建新的基数 PluralFormat

PluralFormat(PluralRules rules, String pattern)

为给定的一组规则和模式创建新的基数 PluralFormat

PluralFormat(ULocale ulocale, PluralRules rules, String pattern)

为给定的一组规则,模式和语言环境创建新的基数 PluralFormat

PluralFormat(ULocale ulocale, PluralRules.PluralType type, String pattern)

为复数类型,模式和区域创建新的 PluralFormat

Public methods

void applyPattern(String pattern)

设置这种复数格式使用的模式。

boolean equals(Object rhs)

指示其他某个对象是否“等于”这一个。

boolean equals(PluralFormat rhs)

如果这等于提供的PluralFormat,则返回true。

final String format(double number)

为给定数字格式化复数消息。

StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)

格式化给定数字的复数消息并将格式化的消息追加到给定的 StringBuffer

int hashCode()

返回对象的哈希码值。

Number parse(String text, ParsePosition parsePosition)

PluralFormat尚不支持此方法。

Object parseObject(String source, ParsePosition pos)

PluralFormat尚不支持此方法。

void setNumberFormat(NumberFormat format)

设置此格式化程序使用的数字格式。

String toPattern()

返回此PluralFormat的模式。

String toString()

返回对象的字符串表示形式。

Inherited methods

From class java.text.Format
From class java.lang.Object

Public constructors

PluralFormat

Added in API level 24
PluralFormat ()

创建一个新的基数号码PluralFormat默认FORMAT语言环境。 此区域设置将用于获取一组复数规则和标准数字格式。

也可以看看:

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale)

为给定的语言环境创建新的基数 PluralFormat

Parameters
ulocale ULocale: the PluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.

PluralFormat

Added in API level 24
PluralFormat (Locale locale)

创建一个新的基数号码 PluralFormat对于给定 Locale

Parameters
locale Locale: the PluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.

PluralFormat

Added in API level 24
PluralFormat (PluralRules rules)

为一组给定的规则创建新的基数PluralFormat 标准数字格式将使用默认的FORMAT区域设置完成。

Parameters
rules PluralRules: defines the behavior of the PluralFormat object.

也可以看看:

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale, 
                PluralRules rules)

为给定的一组规则创建新的基数PluralFormat 标准数字格式将使用给定的语言环境来完成。

Parameters
ulocale ULocale: the default number formatting will be done using this locale.
rules PluralRules: defines the behavior of the PluralFormat object.

PluralFormat

Added in API level 24
PluralFormat (Locale locale, 
                PluralRules rules)

为给定的一组规则创建新的基数PluralFormat 标准数字格式将使用给定的语言环境来完成。

Parameters
locale Locale: the default number formatting will be done using this locale.
rules PluralRules: defines the behavior of the PluralFormat object.

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale, 
                PluralRules.PluralType type)

为复数类型创建一个新的PluralFormat 标准数字格式将使用给定的语言环境来完成。

Parameters
ulocale ULocale: the default number formatting will be done using this locale.
type PluralRules.PluralType: The plural type (e.g., cardinal or ordinal).

PluralFormat

Added in API level 24
PluralFormat (Locale locale, 
                PluralRules.PluralType type)

为复数类型创建一个新的PluralFormat 标准数字格式将使用给定的Locale

Parameters
locale Locale: the default number formatting will be done using this locale.
type PluralRules.PluralType: The plural type (e.g., cardinal or ordinal).

PluralFormat

Added in API level 24
PluralFormat (String pattern)

为给定的模式字符串创建新的基数PluralFormat 默认的FORMAT区域设置将用于获取复数规则集和标准数字格式。

Parameters
pattern String: the pattern for this PluralFormat.
Throws
IllegalArgumentException if the pattern is invalid.

也可以看看:

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale, 
                String pattern)

为给定的模式字符串和语言环境创建新的基数PluralFormat 语言环境将用于获取一组复数规则和标准数字格式。

示例代码:

ULocale locEn = new ULocale("en");
ULocale locSl = new ULocale("sl");

String patEn = "one{dog} other{dogs}";                      // English 'dog'
String patSl = "one{pes} two{psa} few{psi} other{psov}";    // Slovenian translation of dog in Plural Form

// Create a new PluralFormat for a given locale locale and pattern string
PluralFormat plfmtEn = new PluralFormat(locEn, patEn);
PluralFormat plfmtSl = new PluralFormat(locSl, patSl);
// Constructs a MessageFormat for the specified locale and pattern.
MessageFormat msgfmtEn = new MessageFormat("{0,number} {1}", locEn);
MessageFormat msgfmtSl = new MessageFormat("{0,number} {1}", locSl);

final int[] numbers = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102};
System.out.println("Output by using PluralFormat and MessageFormat API\n");
System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian");

// Use MessageFormat.format () to format the objects and appends to the given StringBuffer
for (int num : numbers) {
    StringBuffer msgEn = new StringBuffer();
    StringBuffer msgSl = new StringBuffer();

    msgfmtEn.format(new Object[] {num, plfmtEn.format(num)}, msgEn, new FieldPosition(0));
    msgfmtSl.format(new Object[] {num, plfmtSl.format(num)}, msgSl, new FieldPosition(0));

    System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl);
}

System.out.println();

// Equivalent code with message format pattern
String msgPatEn = "{0,plural, one{# dog} other{# dogs}}";
String msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}";

MessageFormat altMsgfmtEn = new MessageFormat(msgPatEn, locEn);
MessageFormat altMsgfmtSl = new MessageFormat(msgPatSl, locSl);
System.out.println("Same Output by using MessageFormat API only\n");
System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian");
for (int num : numbers) {
    StringBuffer msgEn = new StringBuffer();
    StringBuffer msgSl = new StringBuffer();

    altMsgfmtEn.format(new Object[] {num}, msgEn, new FieldPosition(0));
    altMsgfmtSl.format(new Object[] {num}, msgSl, new FieldPosition(0));

    System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl);
}
/** output of the sample code:
 ********************************************************************
  Number          English         Slovenian
  0               0 dogs          0 psov
  1               1 dog           1 pes
  2               2 dogs          2 psa
  3               3 dogs          3 psi
  4               4 dogs          4 psi
  5               5 dogs          5 psov
  10              10 dogs         10 psov
  100             100 dogs        100 psov
  101             101 dogs        101 pes
  102             102 dogs        102 psa

 *******************************************************************/

Parameters
ulocale ULocale: the PluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
pattern String: the pattern for this PluralFormat.
Throws
IllegalArgumentException if the pattern is invalid.

PluralFormat

Added in API level 24
PluralFormat (PluralRules rules, 
                String pattern)

为给定的一组规则和模式创建新的基数PluralFormat 标准数字格式将使用默认的FORMAT区域设置完成。

Parameters
rules PluralRules: defines the behavior of the PluralFormat object.
pattern String: the pattern for this PluralFormat.
Throws
IllegalArgumentException if the pattern is invalid.

也可以看看:

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale, 
                PluralRules rules, 
                String pattern)

为给定的一组规则,模式和区域创建新的基数 PluralFormat

Parameters
ulocale ULocale: the PluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
rules PluralRules: defines the behavior of the PluralFormat object.
pattern String: the pattern for this PluralFormat.
Throws
IllegalArgumentException if the pattern is invalid.

PluralFormat

Added in API level 24
PluralFormat (ULocale ulocale, 
                PluralRules.PluralType type, 
                String pattern)

为复数类型,模式和语言环境创建一个新的 PluralFormat

Parameters
ulocale ULocale: the PluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
type PluralRules.PluralType: The plural type (e.g., cardinal or ordinal).
pattern String: the pattern for this PluralFormat.
Throws
IllegalArgumentException if the pattern is invalid.

Public methods

applyPattern

Added in API level 24
void applyPattern (String pattern)

设置这种复数格式使用的模式。 该方法解析模式并为复数规则创建格式字符串映射。 模板及其解释在类描述中指定。

Parameters
pattern String: the pattern for this plural format.
Throws
IllegalArgumentException if the pattern is invalid.

equals

Added in API level 24
boolean equals (Object rhs)

指示其他某个对象是否“等于”这一个。

equals方法在非空对象引用上实现等价关系:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

Objectequals方法实现了对象上最可能的等价关系; 也就是说,对于任何非空参考值xy ,当且仅当xy引用同一对象( x == y的值为true )时,此方法返回true

请注意,无论何时覆盖此方法,通常都需要覆盖 hashCode方法,以便维护 hashCode方法的一般合同,该方法声明相等对象必须具有相同的哈希代码。

Parameters
rhs Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

equals

Added in API level 24
boolean equals (PluralFormat rhs)

如果这等于提供的PluralFormat,则返回true。

Parameters
rhs PluralFormat: the PluralFormat to compare against
Returns
boolean true if this equals rhs

format

Added in API level 24
String format (double number)

为给定数字格式化复数消息。

Parameters
number double: a number for which the plural message should be formatted. If no pattern has been applied to this PluralFormat object yet, the formatted number will be returned.
Returns
String the string containing the formatted plural message.

format

Added in API level 24
StringBuffer format (Object number, 
                StringBuffer toAppendTo, 
                FieldPosition pos)

为给定数字格式化复数消息,并将格式化消息追加到给定的 StringBuffer

Parameters
number Object: a number object (instance of Number for which the plural message should be formatted. If no pattern has been applied to this PluralFormat object yet, the formatted number will be returned. Note: If this object is not an instance of Number, the toAppendTo will not be modified.
toAppendTo StringBuffer: the formatted message will be appended to this StringBuffer.
pos FieldPosition: will be ignored by this method.
Returns
StringBuffer the string buffer passed in as toAppendTo, with formatted text appended.
Throws
IllegalArgumentException if number is not an instance of Number

hashCode

Added in API level 24
int hashCode ()

返回对象的哈希码值。 为了散列表的好处而支持此方法,例如由HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

尽可能合理实用,由类Object定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)

Returns
int a hash code value for this object.

parse

Added in API level 24
Number parse (String text, 
                ParsePosition parsePosition)

PluralFormat尚不支持此方法。

Parameters
text String: the string to be parsed.
parsePosition ParsePosition: defines the position where parsing is to begin, and upon return, the position where parsing left off. If the position has not changed upon return, then parsing failed.
Returns
Number nothing because this method is not yet implemented.
Throws
UnsupportedOperationException will always be thrown by this method.

parseObject

Added in API level 24
Object parseObject (String source, 
                ParsePosition pos)

PluralFormat尚不支持此方法。

Parameters
source String: the string to be parsed.
pos ParsePosition: defines the position where parsing is to begin, and upon return, the position where parsing left off. If the position has not changed upon return, then parsing failed.
Returns
Object nothing because this method is not yet implemented.
Throws
UnsupportedOperationException will always be thrown by this method.

setNumberFormat

Added in API level 24
void setNumberFormat (NumberFormat format)

设置此格式化程序使用的数字格式。 如果您需要与区域设置的默认格式化程序不同的数字格式,则只需调用此方法。

Parameters
format NumberFormat: the number format to use.

toPattern

Added in API level 24
String toPattern ()

返回此PluralFormat的模式。

Returns
String the pattern string

toString

Added in API level 24
String toString ()

返回对象的字符串表示形式。 通常, toString方法将返回一个“文本表示”此对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法将返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

Hooray!