PluralFormat supports the creation of internationalized
messages with plural inflection. It is based on plural
selection, i.e. the caller specifies messages for each
plural case that can appear in the user's language and the
PluralFormat selects the appropriate message based on
the number.
Different languages have different ways to inflect
plurals. Creating internationalized messages that include plural
forms is only feasible when the framework is able to handle plural
forms of all languages correctly. ChoiceFormat
doesn't handle this well, because it attaches a number interval to
each message and selects the message whose interval contains a
given number. This can only handle a finite number of
intervals. But in some languages, like Polish, one plural case
applies to infinitely many intervals (e.g., the paucal case applies to
numbers ending with 2, 3, or 4 except those ending with 12, 13, or
14). Thus ChoiceFormat is not adequate.
PluralFormat deals with this by breaking the problem
into two parts:
PluralRules that can define more complex
conditions for a plural case than just a single interval. These plural
rules define both what plural cases exist in a language, and to
which numbers these cases apply.
PluralFormatNote
Typically, plural formatting is done via MessageFormat
with a plural argument type,
rather than using a stand-alone PluralFormat.
This discussion assumes that you use PluralFormat with
a predefined set of plural rules. You can create one using one of
the constructors that takes a ULocale object. To
specify the message pattern, you can either pass it to the
constructor or set it explicitly using the
applyPattern() method. The format()
method takes a number object and selects the message of the
matching plural case. This message will be returned.
The pattern text defines the message output for each plural case of the specified locale. Syntax:
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.
There are 6 predefined case keywords in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and
'other'. You always have to define a message text for the default plural case
"other" which is contained in every rule set.
If you do not specify a message text for a particular plural case, the
message text of the plural case "other" gets assigned to this
plural case.
When formatting, the input number is first matched against the explicitValue clauses.
If there is no exact-number match, then a keyword is selected by calling
the PluralRules with the input number minus the offset.
(The offset defaults to 0 if it is omitted from the pattern string.)
If there is no clause with that keyword, then the "other" clauses is returned.
An unquoted pound sign (#) in the selected sub-message
itself (i.e., outside of arguments nested in the sub-message)
is replaced by the input number minus the offset.
The number-minus-offset value is formatted using a
NumberFormat for the PluralFormat's locale. If you
need special number formatting, you have to use a MessageFormat
and explicitly specify a NumberFormat argument
Note
That argument is formatting without subtracting the offset! If you need a custom format and have a non-zero offset, then you need to pass the number-minus-offset value as a separate parameter.
For a usage example, see the MessageFormat class documentation.
If you need to use PluralFormat with custom rules, you can
create a PluralRules object and pass it to
PluralFormat's constructor. If you also specify a locale in this
constructor, this locale will be used to format the number in the message
texts.
For more information about PluralRules, see
PluralRules.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static interface | PluralFormat.PluralSelector
Interface for selecting PluralFormat keywords for numbers. |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private transient MessagePattern | msgPattern
The MessagePattern which contains the parsed structure of the pattern string. |
| private NumberFormat | numberFormat
This |
| private transient double | offset
The offset to subtract before invoking plural rules. |
| private Map | parsedValues
Obsolete with use of MessagePattern since ICU 4.8. |
| private String | pattern
The applied pattern string. |
| private PluralRules | pluralRules
The plural rules used for plural selection. |
| private transient PluralFormat. | |
| private static final long | |
| private ULocale | ulocale
The locale used for standard number formatting and getting the predefined plural rules (if they were not defined explicitely). |
| Access | Constructor and Description |
|---|---|
| public | |
| public | PluralFormat(ULocale
the ulocale)PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.Creates a new cardinal-number |
| public | PluralFormat(Locale
the locale)PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.Creates a new cardinal-number |
| public | PluralFormat(PluralRules
defines the behavior of the rules)PluralFormat
object.Creates a new cardinal-number |
| public | PluralFormat(ULocale
the default number formatting will be done using this
locale. ulocale, PluralRules defines the behavior of the rules)PluralFormat
object.Creates a new cardinal-number |
| public | PluralFormat(Locale
the default number formatting will be done using this
locale. locale, PluralRules defines the behavior of the rules)PluralFormat
object.Creates a new cardinal-number |
| public | PluralFormat(ULocale
the default number formatting will be done using this
locale. ulocale, PluralRules.The plural type (e.g., cardinal or ordinal). typeCreates a new |
| public | PluralFormat(Locale
the default number formatting will be done using this
locale. locale, PluralRules.The plural type (e.g., cardinal or ordinal). typeCreates a new |
| public | PluralFormat(String
the pattern for this pattern)PluralFormat.Creates a new cardinal-number |
| public | PluralFormat(ULocale
the ulocale, String PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.the pattern for this pattern)PluralFormat.Creates a new cardinal-number |
| public | PluralFormat(PluralRules
defines the behavior of the rules, String PluralFormat
object.the pattern for this pattern)PluralFormat.Creates a new cardinal-number |
| public | PluralFormat(ULocale
the ulocale, PluralRules PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.defines the behavior of the rules, String PluralFormat
object.the pattern for this pattern)PluralFormat.Creates a new cardinal-number |
| public | PluralFormat(ULocale
the ulocale, PluralRules.PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.The plural type (e.g., cardinal or ordinal). type,the pattern for this pattern)PluralFormat.Creates a new |
| pack-priv | PluralFormat(ULocale
the ulocale, PluralRules.PluralFormat will be configured with
rules for this locale. This locale will also be used for standard
number formatting.The plural type (e.g., cardinal or ordinal). type,the pattern for this pattern, NumberFormat PluralFormat.The number formatter to use. numberFormat)Creates a new |
| Modifier and Type | Method and Description |
|---|---|
| public void | applyPattern(String
the pattern for this plural format. pattern)Sets the pattern used by this plural format. |
| public boolean | equals(Object
the reference object with which to compare. rhs)Overrides java. |
| public boolean | Returns: true if this equals rhsthe PluralFormat to compare against rhs)Returns true if this equals the provided PluralFormat. |
| pack-priv static int | Returns: the sub-message start part index.A MessagePattern. pattern, int the index of the first PluralFormat argument style part. partIndex, PluralFormat.the PluralSelector for mapping the number (minus offset) to a keyword. selector,worker object for the selector. context, double a number to be matched to one of the PluralFormat argument's explicit values,
or mapped via the PluralSelector. number)Finds the PluralFormat sub-message for the given number, or the "other" sub-message. |
| public final String | Returns: the string containing the formatted plural message.a number for which the plural message should be formatted.
If no pattern has been applied to this
number)PluralFormat object yet, the formatted number will
be returned.Formats a plural message for a given number. |
| public StringBuffer | Returns: the string buffer passed in as toAppendTo, with formatted text appended.a number object (instance of number, StringBuffer 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 the formatted message will be appended to this
toAppendTo, FieldPosition StringBuffer.will be ignored by this method. pos)Implements abstract java. StringBuffer.
|
| private String | |
| public int | |
| private void | |
| public Number | Returns: nothing because this method is not yet implemented.the string to be parsed. text, 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. parsePosition)This method is not yet supported by |
| public Object | Returns: nothing because this method is not yet implemented.the string to be parsed. source, 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. pos)Implements abstract java. PluralFormat.
|
| pack-priv String | Returns: Returns the PluralRules type. For example, it could be "zero", "one", "two", "few", "many" or "other")the string to be parsed. source, RbnfLenientScanner scanner, FieldPosition defines the position where parsing is to begin,
and upon return, the position where parsing left off. If the position
is a negative index, then parsing failed. pos)
Deprecated
as a consequence of
This method returns the PluralRules type found from parsing.
RbnfLenientScanner being deprecated.
|
| private void | |
| private void | |
| public void | setLocale(ULocale
the ulocale)ULocale used to configure the
formatter. If ulocale is null, the
default FORMAT locale will be used.
Deprecated
ICU 50 This method clears the pattern and might create
a different kind of PluralRules instance;
use one of the constructors to create a new instance instead.
Sets the locale used by this PluraFormat object.
|
| public void | setNumberFormat(NumberFormat
the number format to use. format)Sets the number format used by this formatter. |
| public String | |
| public String |
| msgPattern | back to summary |
|---|---|
| private transient MessagePattern msgPattern The MessagePattern which contains the parsed structure of the pattern string. | |
| numberFormat | back to summary |
|---|---|
| private NumberFormat numberFormat This | |
| offset | back to summary |
|---|---|
| private transient double offset The offset to subtract before invoking plural rules. | |
| parsedValues | back to summary |
|---|---|
| private Map<String, String> parsedValues Obsolete with use of MessagePattern since ICU 4.8. Used to be:
The format messages for each plural case. It is a mapping:
| |
| pattern | back to summary |
|---|---|
| private String pattern The applied pattern string. | |
| pluralRules | back to summary |
|---|---|
| private PluralRules pluralRules The plural rules used for plural selection. | |
| pluralRulesWrapper | back to summary |
|---|---|
| private transient PluralFormat. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| ulocale | back to summary |
|---|---|
| private ULocale ulocale The locale used for standard number formatting and getting the predefined plural rules (if they were not defined explicitely). | |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat() Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale) Creates a new cardinal-number | |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(Locale locale) Creates a new cardinal-number | |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(PluralRules rules) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale, PluralRules rules) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(Locale locale, PluralRules rules) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale, PluralRules. Creates a new
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(Locale locale, PluralRules. Creates a new
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(String pattern) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale, String pattern) Creates a new cardinal-number Example code:{@. jcite com.ibm.icu.samples.text.pluralformat.PluralFormatSample:---PluralFormatExample}
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(PluralRules rules, String pattern) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale, PluralRules rules, String pattern) Creates a new cardinal-number
| |
| PluralFormat | back to summary |
|---|---|
| public PluralFormat(ULocale ulocale, PluralRules. Creates a new
| |
| PluralFormat | back to summary |
|---|---|
| pack-priv PluralFormat(ULocale ulocale, PluralRules. Creates a new
| |
| applyPattern | back to summary |
|---|---|
| public void applyPattern(String pattern) Sets the pattern used by this plural format. The method parses the pattern and creates a map of format strings for the plural rules. Patterns and their interpretation are specified in the class description.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object rhs) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| equals | back to summary |
|---|---|
| public boolean equals(PluralFormat rhs) Returns true if this equals the provided PluralFormat.
| |
| findSubMessage | back to summary |
|---|---|
| pack-priv static int findSubMessage(MessagePattern pattern, int partIndex, PluralFormat. Finds the PluralFormat sub-message for the given number, or the "other" sub-message.
| |
| format | back to summary |
|---|---|
| public final String format(double number) Formats a plural message for a given number. | |
| format | back to summary |
|---|---|
| public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) Implements abstract java. Formats a plural message for a given number and appends the formatted
message to the given
| |
| format | back to summary |
|---|---|
| private String format(Number numberObject, double number) | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| init | back to summary |
|---|---|
| private void init(PluralRules rules, PluralRules. | |
| parse | back to summary |
|---|---|
| public Number parse(String text, ParsePosition parsePosition) This method is not yet supported by
| |
| parseObject | back to summary |
|---|---|
| public Object parseObject(String source, ParsePosition pos) Implements abstract java. This method is not yet supported by
| |
| parseType | back to summary |
|---|---|
| pack-priv String parseType(String source, RbnfLenientScanner scanner, FieldPosition pos)
Deprecated as a consequence of This method returns the PluralRules type found from parsing.
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException | |
| resetPattern | back to summary |
|---|---|
| private void resetPattern() | |
| setLocale | back to summary |
|---|---|
| public void setLocale(ULocale ulocale)
Deprecated ICU 50 This method clears the pattern and might create a different kind of PluralRules instance; use one of the constructors to create a new instance instead. Sets the locale used by this Note Calling this method resets this
| |
| setNumberFormat | back to summary |
|---|---|
| public void setNumberFormat(NumberFormat format) Sets the number format used by this formatter. You only need to call this if you want a different number format than the default formatter for the locale.
| |
| toPattern | back to summary |
|---|---|
| public String toPattern() Returns the pattern for this PluralFormat. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Method and Description |
|---|---|
| public String |
| select | back to summary |
|---|---|
| public String select(Object context, double number) Given a number, returns the appropriate PluralFormat keyword. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public String | select(Object
worker object for the selector. context, double The number to be plural-formatted. number)Implements com. |
| PluralSelectorAdapter | back to summary |
|---|---|
| private PluralSelectorAdapter() | |
| select | back to summary |
|---|---|
| public String select(Object context, double number) Implements com. Doc from com. Given a number, returns the appropriate PluralFormat keyword. | |