public class PluralFormat extends UFormat
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.
PluralFormat
Note: 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:
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.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: seeMessageFormat
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
.
UFormat.SpanField
Format.Field
Constructor and Description |
---|
PluralFormat()
Creates a new cardinal-number
PluralFormat for the default FORMAT locale. |
PluralFormat(Locale locale)
Creates a new cardinal-number
PluralFormat for a given
Locale . |
PluralFormat(Locale locale,
PluralRules.PluralType type)
Creates a new
PluralFormat for the plural type. |
PluralFormat(Locale locale,
PluralRules rules)
Creates a new cardinal-number
PluralFormat for a given set of rules. |
PluralFormat(PluralRules rules)
Creates a new cardinal-number
PluralFormat for a given set of rules. |
PluralFormat(PluralRules rules,
String pattern)
Creates a new cardinal-number
PluralFormat for a given set of rules and a
pattern. |
PluralFormat(String pattern)
Creates a new cardinal-number
PluralFormat for a given pattern string. |
PluralFormat(ULocale ulocale)
Creates a new cardinal-number
PluralFormat for a given locale. |
PluralFormat(ULocale ulocale,
PluralRules.PluralType type)
Creates a new
PluralFormat for the plural type. |
PluralFormat(ULocale ulocale,
PluralRules.PluralType type,
String pattern)
Creates a new
PluralFormat for a plural type, a
pattern and a locale. |
PluralFormat(ULocale ulocale,
PluralRules rules)
Creates a new cardinal-number
PluralFormat for a given set of rules. |
PluralFormat(ULocale ulocale,
PluralRules rules,
String pattern)
Creates a new cardinal-number
PluralFormat for a given set of rules, a
pattern and a locale. |
PluralFormat(ULocale ulocale,
String pattern)
Creates a new cardinal-number
PluralFormat for a given pattern string and
locale. |
Modifier and Type | Method and Description |
---|---|
void |
applyPattern(String pattern)
Sets the pattern used by this plural format.
|
boolean |
equals(Object rhs) |
boolean |
equals(PluralFormat rhs)
Returns true if this equals the provided PluralFormat.
|
String |
format(double number)
Formats a plural message for a given number.
|
StringBuffer |
format(Object number,
StringBuffer toAppendTo,
FieldPosition pos)
Formats a plural message for a given number and appends the formatted
message to the given
StringBuffer . |
int |
hashCode() |
Number |
parse(String text,
ParsePosition parsePosition)
This method is not yet supported by
PluralFormat . |
Object |
parseObject(String source,
ParsePosition pos)
This method is not yet supported by
PluralFormat . |
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.
|
void |
setNumberFormat(NumberFormat format)
Sets the number format used by this formatter.
|
String |
toPattern()
Returns the pattern for this PluralFormat.
|
String |
toString() |
clone, format, formatToCharacterIterator, parseObject
public PluralFormat()
PluralFormat
for the default FORMAT
locale.
This locale will be used to get the set of plural rules and for standard
number formatting.ULocale.Category.FORMAT
public PluralFormat(ULocale ulocale)
PluralFormat
for a given locale.ulocale
- the PluralFormat
will be configured with
rules for this locale. This locale will also be used for standard
number formatting.public PluralFormat(Locale locale)
PluralFormat
for a given
Locale
.locale
- the PluralFormat
will be configured with
rules for this locale. This locale will also be used for standard
number formatting.public PluralFormat(PluralRules rules)
PluralFormat
for a given set of rules.
The standard number formatting will be done using the default FORMAT
locale.rules
- defines the behavior of the PluralFormat
object.ULocale.Category.FORMAT
public PluralFormat(ULocale ulocale, PluralRules rules)
PluralFormat
for a given set of rules.
The standard number formatting will be done using the given locale.ulocale
- the default number formatting will be done using this
locale.rules
- defines the behavior of the PluralFormat
object.public PluralFormat(Locale locale, PluralRules rules)
PluralFormat
for a given set of rules.
The standard number formatting will be done using the given locale.locale
- the default number formatting will be done using this
locale.rules
- defines the behavior of the PluralFormat
object.public PluralFormat(ULocale ulocale, PluralRules.PluralType type)
PluralFormat
for the plural type.
The standard number formatting will be done using the given locale.ulocale
- the default number formatting will be done using this
locale.type
- The plural type (e.g., cardinal or ordinal).public PluralFormat(Locale locale, PluralRules.PluralType type)
PluralFormat
for the plural type.
The standard number formatting will be done using the given Locale
.locale
- the default number formatting will be done using this
locale.type
- The plural type (e.g., cardinal or ordinal).public PluralFormat(String pattern)
PluralFormat
for a given pattern string.
The default FORMAT
locale will be used to get the set of plural rules and for
standard number formatting.pattern
- the pattern for this PluralFormat
.IllegalArgumentException
- if the pattern is invalid.ULocale.Category.FORMAT
public PluralFormat(ULocale ulocale, String pattern)
PluralFormat
for a given pattern string and
locale.
The locale will be used to get the set of plural rules and for
standard number formatting.
Example code:
import java.text.FieldPosition; import com.ibm.icu.text.MessageFormat; import com.ibm.icu.text.PluralFormat; import com.ibm.icu.util.ULocale; 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 *******************************************************************/
ulocale
- the PluralFormat
will be configured with
rules for this locale. This locale will also be used for standard
number formatting.pattern
- the pattern for this PluralFormat
.IllegalArgumentException
- if the pattern is invalid.public PluralFormat(PluralRules rules, String pattern)
PluralFormat
for a given set of rules and a
pattern.
The standard number formatting will be done using the default FORMAT
locale.rules
- defines the behavior of the PluralFormat
object.pattern
- the pattern for this PluralFormat
.IllegalArgumentException
- if the pattern is invalid.ULocale.Category.FORMAT
public PluralFormat(ULocale ulocale, PluralRules rules, String pattern)
PluralFormat
for a given set of rules, a
pattern and a locale.ulocale
- the PluralFormat
will be configured with
rules for this locale. This locale will also be used for standard
number formatting.rules
- defines the behavior of the PluralFormat
object.pattern
- the pattern for this PluralFormat
.IllegalArgumentException
- if the pattern is invalid.public PluralFormat(ULocale ulocale, PluralRules.PluralType type, String pattern)
PluralFormat
for a plural type, a
pattern and a locale.ulocale
- the PluralFormat
will be configured with
rules for this locale. This locale will also be used for standard
number formatting.type
- The plural type (e.g., cardinal or ordinal).pattern
- the pattern for this PluralFormat
.IllegalArgumentException
- if the pattern is invalid.public void applyPattern(String pattern)
pattern
- the pattern for this plural format.IllegalArgumentException
- if the pattern is invalid.public String toPattern()
public final String format(double number)
number
- 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.public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)
StringBuffer
.format
in class Format
number
- 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
- the formatted message will be appended to this
StringBuffer
.pos
- will be ignored by this method.IllegalArgumentException
- if number is not an instance of Numberpublic Number parse(String text, ParsePosition parsePosition)
PluralFormat
.text
- the string to be parsed.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.UnsupportedOperationException
- will always be thrown by this method.public Object parseObject(String source, ParsePosition pos)
PluralFormat
.parseObject
in class Format
source
- the string to be parsed.pos
- 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.UnsupportedOperationException
- will always be thrown by this method.@Deprecated public void setLocale(ULocale ulocale)
PluraFormat
object.
Note: Calling this method resets this PluraFormat
object,
i.e., a pattern that was applied previously will be removed,
and the NumberFormat is set to the default number format for
the locale. The resulting format behaves the same as one
constructed from PluralFormat(ULocale, PluralRules.PluralType)
with PluralType.CARDINAL.ulocale
- the ULocale
used to configure the
formatter. If ulocale
is null
, the
default FORMAT
locale will be used.ULocale.Category.FORMAT
public void setNumberFormat(NumberFormat format)
format
- the number format to use.public boolean equals(PluralFormat rhs)
rhs
- the PluralFormat to compare againstCopyright © 2016 Unicode, Inc. and others.