Class PluralFormat
- java.lang.Object
-
- java.text.Format
-
- com.ibm.icu.text.UFormat
-
- com.ibm.icu.text.PluralFormat
-
- All Implemented Interfaces:
Serializable,Cloneable
public class PluralFormat extends UFormat
PluralFormatsupports 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 thePluralFormatselects the appropriate message based on the number.The Problem of Plural Forms in Internationalized Messages
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.
ChoiceFormatdoesn'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). ThusChoiceFormatis not adequate.PluralFormatdeals with this by breaking the problem into two parts:- It uses
PluralRulesthat 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. - It provides predefined plural rules for many languages. Thus, the programmer need not worry about the plural cases of a language and does not have to define the plural cases; they can simply use the predefined keywords. The whole plural formatting of messages can be done using localized patterns from resource bundles. For predefined plural rules, see the CLDR Language Plural Rules page at http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
Usage of
PluralFormatNote: Typically, plural formatting is done via
MessageFormatwith apluralargument type, rather than using a stand-alonePluralFormat.This discussion assumes that you use
PluralFormatwith a predefined set of plural rules. You can create one using one of the constructors that takes aULocaleobject. To specify the message pattern, you can either pass it to the constructor or set it explicitly using theapplyPattern()method. Theformat()method takes a number object and selects the message of the matching plural case. This message will be returned.Patterns and Their Interpretation
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: seeMessageFormatThere 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
PluralRuleswith 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 (
For a usage example, see the#) 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 aNumberFormatfor thePluralFormat's locale. If you need special number formatting, you have to use aMessageFormatand explicitly specify aNumberFormatargument. 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.MessageFormatclass documentation.Defining Custom Plural Rules
If you need to use
PluralFormatwith custom rules, you can create aPluralRulesobject and pass it toPluralFormat'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, seePluralRules.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
-
Constructor Summary
Constructors Constructor Description PluralFormat()Creates a new cardinal-numberPluralFormatfor the defaultFORMATlocale.PluralFormat(PluralRules rules)Creates a new cardinal-numberPluralFormatfor a given set of rules.PluralFormat(PluralRules rules, String pattern)Creates a new cardinal-numberPluralFormatfor a given set of rules and a pattern.PluralFormat(ULocale ulocale)Creates a new cardinal-numberPluralFormatfor a given locale.PluralFormat(ULocale ulocale, PluralRules rules)Creates a new cardinal-numberPluralFormatfor a given set of rules.PluralFormat(ULocale ulocale, PluralRules.PluralType type)Creates a newPluralFormatfor the plural type.PluralFormat(ULocale ulocale, PluralRules.PluralType type, String pattern)Creates a newPluralFormatfor a plural type, a pattern and a locale.PluralFormat(ULocale ulocale, PluralRules rules, String pattern)Creates a new cardinal-numberPluralFormatfor a given set of rules, a pattern and a locale.PluralFormat(ULocale ulocale, String pattern)Creates a new cardinal-numberPluralFormatfor a given pattern string and locale.PluralFormat(String pattern)Creates a new cardinal-numberPluralFormatfor a given pattern string.PluralFormat(Locale locale)Creates a new cardinal-numberPluralFormatfor a given JDK locale.PluralFormat(Locale locale, PluralRules rules)Creates a new cardinal-numberPluralFormatfor a given set of rules.PluralFormat(Locale locale, PluralRules.PluralType type)Creates a newPluralFormatfor the plural type.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidapplyPattern(String pattern)Sets the pattern used by this plural format.booleanequals(PluralFormat rhs)Returns true if this equals the provided PluralFormat.booleanequals(Object rhs)Compares this instance with the specified object and indicates if they are equal.Stringformat(double number)Formats a plural message for a given number.StringBufferformat(Object number, StringBuffer toAppendTo, FieldPosition pos)Formats a plural message for a given number and appends the formatted message to the givenStringBuffer.inthashCode()Returns an integer hash code for this object.Numberparse(String text, ParsePosition parsePosition)This method is not yet supported byPluralFormat.ObjectparseObject(String source, ParsePosition pos)This method is not yet supported byPluralFormat.voidsetLocale(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.voidsetNumberFormat(NumberFormat format)Sets the number format used by this formatter.StringtoPattern()Returns the pattern for this PluralFormat.StringtoString()Returns a string containing a concise, human-readable description of this object.-
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
-
-
-
Constructor Detail
-
PluralFormat
public PluralFormat()
Creates a new cardinal-numberPluralFormatfor the defaultFORMATlocale. This locale will be used to get the set of plural rules and for standard number formatting.- See Also:
ULocale.Category.FORMAT
-
PluralFormat
public PluralFormat(ULocale ulocale)
Creates a new cardinal-numberPluralFormatfor a given locale.- Parameters:
ulocale- thePluralFormatwill be configured with rules for this locale. This locale will also be used for standard number formatting.
-
PluralFormat
public PluralFormat(Locale locale)
Creates a new cardinal-numberPluralFormatfor a given JDK locale.- Parameters:
locale- thePluralFormatwill be configured with rules for this locale. This locale will also be used for standard number formatting.
-
PluralFormat
public PluralFormat(PluralRules rules)
Creates a new cardinal-numberPluralFormatfor a given set of rules. The standard number formatting will be done using the defaultFORMATlocale.- Parameters:
rules- defines the behavior of thePluralFormatobject.- See Also:
ULocale.Category.FORMAT
-
PluralFormat
public PluralFormat(ULocale ulocale, PluralRules rules)
Creates a new cardinal-numberPluralFormatfor a given set of rules. The standard number formatting will be done using the given locale.- Parameters:
ulocale- the default number formatting will be done using this locale.rules- defines the behavior of thePluralFormatobject.
-
PluralFormat
public PluralFormat(Locale locale, PluralRules rules)
Creates a new cardinal-numberPluralFormatfor a given set of rules. The standard number formatting will be done using the given locale.- Parameters:
locale- the default number formatting will be done using this locale.rules- defines the behavior of thePluralFormatobject.
-
PluralFormat
public PluralFormat(ULocale ulocale, PluralRules.PluralType type)
Creates a newPluralFormatfor the plural type. The standard number formatting will be done using the given locale.- Parameters:
ulocale- the default number formatting will be done using this locale.type- The plural type (e.g., cardinal or ordinal).
-
PluralFormat
public PluralFormat(Locale locale, PluralRules.PluralType type)
Creates a newPluralFormatfor the plural type. The standard number formatting will be done using the given JDK locale.- Parameters:
locale- the default number formatting will be done using this locale.type- The plural type (e.g., cardinal or ordinal).
-
PluralFormat
public PluralFormat(String pattern)
Creates a new cardinal-numberPluralFormatfor a given pattern string. The defaultFORMATlocale will be used to get the set of plural rules and for standard number formatting.- Parameters:
pattern- the pattern for thisPluralFormat.- Throws:
IllegalArgumentException- if the pattern is invalid.- See Also:
ULocale.Category.FORMAT
-
PluralFormat
public PluralFormat(ULocale ulocale, String pattern)
Creates a new cardinal-numberPluralFormatfor 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:{@.jcite com.ibm.icu.samples.text.pluralformat.PluralFormatSample:---PluralFormatExample}
- Parameters:
ulocale- thePluralFormatwill be configured with rules for this locale. This locale will also be used for standard number formatting.pattern- the pattern for thisPluralFormat.- Throws:
IllegalArgumentException- if the pattern is invalid.
-
PluralFormat
public PluralFormat(PluralRules rules, String pattern)
Creates a new cardinal-numberPluralFormatfor a given set of rules and a pattern. The standard number formatting will be done using the defaultFORMATlocale.- Parameters:
rules- defines the behavior of thePluralFormatobject.pattern- the pattern for thisPluralFormat.- Throws:
IllegalArgumentException- if the pattern is invalid.- See Also:
ULocale.Category.FORMAT
-
PluralFormat
public PluralFormat(ULocale ulocale, PluralRules rules, String pattern)
Creates a new cardinal-numberPluralFormatfor a given set of rules, a pattern and a locale.- Parameters:
ulocale- thePluralFormatwill be configured with rules for this locale. This locale will also be used for standard number formatting.rules- defines the behavior of thePluralFormatobject.pattern- the pattern for thisPluralFormat.- Throws:
IllegalArgumentException- if the pattern is invalid.
-
PluralFormat
public PluralFormat(ULocale ulocale, PluralRules.PluralType type, String pattern)
Creates a newPluralFormatfor a plural type, a pattern and a locale.- Parameters:
ulocale- thePluralFormatwill 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 thisPluralFormat.- Throws:
IllegalArgumentException- if the pattern is invalid.
-
-
Method Detail
-
applyPattern
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.- Parameters:
pattern- the pattern for this plural format.- Throws:
IllegalArgumentException- if the pattern is invalid.
-
toPattern
public String toPattern()
Returns the pattern for this PluralFormat.- Returns:
- the pattern string
-
format
public final String format(double number)
Formats a plural message for a given number.- Parameters:
number- a number for which the plural message should be formatted. If no pattern has been applied to thisPluralFormatobject yet, the formatted number will be returned.- Returns:
- the string containing the formatted plural message.
-
format
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)
Formats a plural message for a given number and appends the formatted message to the givenStringBuffer.- Specified by:
formatin classFormat- Parameters:
number- a number object (instance ofNumberfor which the plural message should be formatted. If no pattern has been applied to thisPluralFormatobject yet, the formatted number will be returned. Note: If this object is not an instance ofNumber, thetoAppendTowill not be modified.toAppendTo- the formatted message will be appended to thisStringBuffer.pos- will be ignored by this method.- Returns:
- the string buffer passed in as toAppendTo, with formatted text appended.
- Throws:
IllegalArgumentException- if number is not an instance of Number
-
parse
public Number parse(String text, ParsePosition parsePosition)
This method is not yet supported byPluralFormat.- Parameters:
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.- Returns:
- nothing because this method is not yet implemented.
- Throws:
UnsupportedOperationException- will always be thrown by this method.
-
parseObject
public Object parseObject(String source, ParsePosition pos)
This method is not yet supported byPluralFormat.- Specified by:
parseObjectin classFormat- Parameters:
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.- Returns:
- nothing because this method is not yet implemented.
- Throws:
UnsupportedOperationException- will always be thrown by this method.
-
setLocale
@Deprecated 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 thisPluraFormatobject. Note: Calling this method resets thisPluraFormatobject, 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 fromPluralFormat(ULocale, PluralRules.PluralType)with PluralType.CARDINAL.- Parameters:
ulocale- theULocaleused to configure the formatter. Ifulocaleisnull, the defaultFORMATlocale will be used.- See Also:
ULocale.Category.FORMAT
-
setNumberFormat
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.- Parameters:
format- the number format to use.
-
equals
public boolean equals(Object rhs)
Compares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Overrides:
equalsin classObject- Parameters:
rhs- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
equals
public boolean equals(PluralFormat rhs)
Returns true if this equals the provided PluralFormat.- Parameters:
rhs- the PluralFormat to compare against- Returns:
- true if this equals rhs
-
hashCode
public int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
-