# PluralFormat Class Reference

`#include <plurfmt.h>`

Inheritance diagram for PluralFormat:

## Public Member Functions
| PluralFormat (UErrorCode &status)
| Creates a new `PluralFormat` for the default locale.
| PluralFormat (const Locale &locale, UErrorCode &status)
| Creates a new `PluralFormat` for a given locale.
| PluralFormat (const PluralRules &rules, UErrorCode &status)
| Creates a new `PluralFormat` for a given set of rules.
| PluralFormat (const Locale &locale, const PluralRules &rules, UErrorCode &status)
| Creates a new `PluralFormat` for a given set of rules.
| PluralFormat (const UnicodeString &pattern, UErrorCode &status)
| Creates a new `PluralFormat` for a given pattern string.
| PluralFormat (const Locale &locale, const UnicodeString &pattern, UErrorCode &status)
| Creates a new `PluralFormat` for a given pattern string and locale.
| PluralFormat (const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
| Creates a new `PluralFormat` for a given set of rules, a pattern and a locale.
| PluralFormat (const Locale &locale, const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
| Creates a new `PluralFormat` for a given set of rules, a pattern and a locale.
| PluralFormat (const PluralFormat &other)
| copy constructor.
virtual | ~PluralFormat ()
| Destructor.
void | applyPattern (const UnicodeString &pattern, UErrorCode &status)
| Sets the pattern used by this plural format.
UnicodeString | format (int32_t number, UErrorCode &status) const
| Formats a plural message for a given number.
UnicodeString | format (double number, UErrorCode &status) const
| Formats a plural message for a given number.
UnicodeString & | format (int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Formats a plural message for a given number.
UnicodeString & | format (double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Formats a plural message for a given number.
void | setLocale (const Locale &locale, UErrorCode &status)
| Sets the locale used by this `PluraFormat` object.
void | setNumberFormat (const NumberFormat *format, UErrorCode &status)
| Sets the number format used by this formatter.
PluralFormat & | operator= (const PluralFormat &other)
| Assignment operator.
virtual UBool | operator== (const Format &other) const
| Return true if another object is semantically equal to this one.
virtual UBool | operator!= (const Format &other) const
| Return true if another object is semantically unequal to this one.
virtual Format * | clone (void) const
| Clones this Format object polymorphically.
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Redeclared Format method.
UnicodeString & | toPattern (UnicodeString &appendTo)
| Returns the pattern from applyPattern() or constructor().
virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
| This method is not yet supported by `PluralFormat`.
virtual UClassID | getDynamicClassID () const
| ICU "poor man's RTTI", returns a UClassID for the actual class.
## Static Public Member Functions
static UClassID | getStaticClassID (void)
| ICU "poor man's RTTI", returns a UClassID for this class.

## Detailed Description

`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 users language and the `PluralFormat` selects 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. `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., paucal 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:

- It uses `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.
- It provides predefined plural rules for many locales. Thus, the programmer need not worry about the plural cases of a language. On the flip side, the localizer does not have to specify the plural cases; he can simply use the predefined keywords. The whole plural formatting of messages can be done using localized patterns from resource bundles.

#### Usage of `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 `locale` 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.

##### Patterns and Their Interpretation

The pattern text defines the message output for each plural case of the used locale. The pattern is a sequence of `caseKeyword{message}` clauses, separated by white space characters. Each clause assigns the message `message` to the plural case identified by `caseKeyword`.

You always have to define a message text for the default plural case "<code>other</code>" which is contained in every rule set. If the plural rules of the `PluralFormat` object do not contain a plural case identified by `caseKeyword`, U_DEFAULT_KEYWORD_MISSING will be set to status. If you do not specify a message text for a particular plural case, the message text of the plural case "<code>other</code>" gets assigned to this plural case. If you specify more than one message for the same plural case, U_DUPLICATE_KEYWORD will be set to status.
Spaces between `caseKeyword` and `message` will be ignored; spaces within `message` will be preserved.

The message text for a particular plural case may contain other message format patterns. `PluralFormat` preserves these so that you can use the strings produced by `PluralFormat` with other formatters. If you are using `PluralFormat` inside a `MessageFormat` pattern, `MessageFormat` will automatically evaluate the resulting format pattern.
Thus, curly braces (`{`, `}`) are only allowed in message texts to define a nested format pattern.
The pound sign (`#`) will be interpreted as the number placeholder in the message text, if it is not contained in curly braces (to preserve `NumberFormat` patterns). `PluralFormat` will replace each of those pound signs by the number passed to the `format()` method. It will be formatted using a `NumberFormat` for the `PluralFormat`'s locale. If you need special number formatting, you have to explicitly specify a `NumberFormat` for the `PluralFormat` to use. Example

     UErrorCode status = U_ZERO_ERROR;
     MessageFormat* msgFmt = new MessageFormat(UnicodeString("{0, plural,
       one{{0, number, C''est #,##0.0#  fichier}} other {Ce sont # fichiers}} dans la liste."),
       Locale("fr"), status);
     if (U_FAILURE(status)) {
         return;
     }
     Formattable args1[] = {(int32_t)0};
     Formattable args2[] = {(int32_t)3};
     FieldPosition ignore(FieldPosition::DONT_CARE);
     UnicodeString result;
     msgFmt->format(args1, 1, result, ignore, status);
     cout << result << endl;
     result.remove();
     msgFmt->format(args2, 1, result, ignore, status);
     cout << result << endl;

Produces the output:
`C'est 0,0 fichier dans la liste.`
`Ce sont 3 fichiers dans la liste."`

Note:
Currently `PluralFormat` does not make use of quotes like `MessageFormat`. If you use plural format strings with `MessageFormat` and want to use a quote sign "<code>'</code>", you have to write "<code>''</code>". `MessageFormat` unquotes this pattern and passes the unquoted pattern to `PluralFormat`. It's a bit trickier if you use nested formats that do quoting. In the example above, we wanted to insert "<code>'</code>" in the number format pattern. Since `NumberFormat` supports quotes, we had to insert "<code>''</code>". But since `MessageFormat` unquotes the pattern before it gets passed to `PluralFormat`, we have to double these quotes, i.e. write "<code>''''</code>".

#### Defining Custom Plural Rules

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.

ported from Java

Stable:
  ICU 4.0

Definition at line 174 of file plurfmt.h.

## Constructor & Destructor Documentation

PluralFormat::PluralFormat | ( | UErrorCode & | status | ) |

Creates a new `PluralFormat` for the default locale.

This locale will be used to get the set of plural rules and for standard number formatting.

Parameters:

  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const Locale & | locale,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for a given locale.

Parameters:

  | locale | the `PluralFormat` will be configured with rules for this locale. This locale will also be used for standard number formatting.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const PluralRules & | rules,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for a given set of rules.

The standard number formatting will be done using the default locale.

Parameters:

  | rules | defines the behavior of the `PluralFormat` object.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const Locale & | locale,
|  | const PluralRules & | rules,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for 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 the `PluralFormat` object.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const UnicodeString & | pattern,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for a given pattern string.

The default locale will be used to get the set of plural rules and for standard number formatting.

Parameters:

  | pattern | the pattern for this `PluralFormat`. errors are returned to status if the pattern is invalid.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const Locale & | locale,
|  | const UnicodeString & | pattern,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `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.

Parameters:

  | locale | 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`. errors are returned to status if the pattern is invalid.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const PluralRules & | rules,
|  | const UnicodeString & | pattern,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for a given set of rules, a pattern and a locale.

Parameters:

  | rules | defines the behavior of the `PluralFormat` object.
  | pattern | the pattern for this `PluralFormat`. errors are returned to status if the pattern is invalid.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const Locale & | locale,
|  | const PluralRules & | rules,
|  | const UnicodeString & | pattern,
|  | UErrorCode & | status |
| ) |  |  |

Creates a new `PluralFormat` for a given set of rules, a pattern and a locale.

Parameters:

  | locale | 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`. errors are returned to status if the pattern is invalid.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

PluralFormat::PluralFormat | ( | const PluralFormat & | other | ) |

copy constructor.

Stable:
  ICU 4.0

virtual PluralFormat::~PluralFormat | ( |  | ) | `[virtual]`

Destructor.

Stable:
  ICU 4.0

## Member Function Documentation

void PluralFormat::applyPattern | ( | const UnicodeString & | pattern,
|  | UErrorCode & | status |
| ) |  |  |

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 errors are returned to status if the pattern is invalid.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

virtual Format* PluralFormat::clone | ( | void |  | ) | const`[virtual]`

Clones this Format object polymorphically.

The caller owns the result and should delete it when done.

Stable:
  ICU 4.0

Implements Format.

UnicodeString& PluralFormat::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status |
| ) |  |  | const`[virtual]`

Redeclared Format method.

Parameters:

  | obj | The object to be formatted into a string.
  | appendTo | output parameter to receive result. Result is appended to existing contents.
  | pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  | status | output param filled with success/failure status.

Returns:
  Reference to 'appendTo' parameter.

Stable:
  ICU 4.0

Implements Format.

UnicodeString& PluralFormat::format | ( | double | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status |
| ) |  |  | const

Formats a plural message for a given number.

Parameters:

  | number | a number for which the plural message should be formatted for. If no pattern has been applied to this `PluralFormat` object yet, the formatted number will be returned.
  | appendTo | output parameter to receive result. result is appended to existing contents.
  | pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Returns:
  the string containing the formatted plural message.

Stable:
  ICU 4.0

UnicodeString& PluralFormat::format | ( | int32_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status |
| ) |  |  | const

Formats a plural message for a given number.

Parameters:

  | number | a number for which the plural message should be formatted for. If no pattern has been applied to this `PluralFormat` object yet, the formatted number will be returned.
  | appendTo | output parameter to receive result. result is appended to existing contents.
  | pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Returns:
  the string containing the formatted plural message.

Stable:
  ICU 4.0

UnicodeString PluralFormat::format | ( | double | number,
|  | UErrorCode & | status |
| ) |  |  | const

Formats a plural message for a given number.

Parameters:

  | number | a number for which the plural message should be formatted for. If no pattern has been applied to this PluralFormat object yet, the formatted number will be returned.
  | status | output param set to success or failure code on exit, which must not indicate a failure before the function call.

Returns:
  the string containing the formatted plural message.

Stable:
  ICU 4.0

UnicodeString PluralFormat::format | ( | int32_t | number,
|  | UErrorCode & | status |
| ) |  |  | const

Formats a plural message for a given number.

Parameters:

  | number | a number for which the plural message should be formatted for. If no pattern has been applied to this `PluralFormat` object yet, the formatted number will be returned.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Returns:
  the string containing the formatted plural message.

Stable:
  ICU 4.0

virtual UClassID PluralFormat::getDynamicClassID | ( |  | ) | const`[virtual]`

ICU "poor man's RTTI", returns a UClassID for the actual class.

Stable:
  ICU 4.0

Implements Format.

static UClassID PluralFormat::getStaticClassID | ( | void |  | ) | `[static]`

ICU "poor man's RTTI", returns a UClassID for this class.

Stable:
  ICU 4.0

virtual UBool PluralFormat::operator!= | ( | const Format & | other | ) | const`[virtual]`

Return true if another object is semantically unequal to this one.

Parameters:

  | other | the PluralFormat object to be compared with.

Returns:
  true if other is semantically unequal to this.

Stable:
  ICU 4.0

Reimplemented from Format.

PluralFormat& PluralFormat::operator= | ( | const PluralFormat & | other | ) |

Assignment operator.

Parameters:

  | other | the PluralFormat object to copy from.

Stable:
  ICU 4.0

virtual UBool PluralFormat::operator== | ( | const Format & | other | ) | const`[virtual]`

Return true if another object is semantically equal to this one.

Parameters:

  | other | the PluralFormat object to be compared with.

Returns:
  true if other is semantically equal to this.

Stable:
  ICU 4.0

Implements Format.

virtual void PluralFormat::parseObject | ( | const UnicodeString & | source,
|  | Formattable & | result,
|  | ParsePosition & | parse_pos |
| ) |  |  | const`[virtual]`

This method is not yet supported by `PluralFormat`.

Before calling, set parse_pos.index to the offset you want to start parsing at in the source. After calling, parse_pos.index is the end of the text you parsed. If error occurs, index is unchanged.

When parsing, leading whitespace is discarded (with a successful parse), while trailing whitespace is left as is.

See Format::parseObject() for more.

Parameters:

  | source | The string to be parsed into an object.
  | result | Formattable to be set to the parse result. If parse fails, return contents are undefined.
  | parse_pos | The position to start parsing at. Upon return this param is set to the position after the last character successfully parsed. If the source is not parsed successfully, this param will remain unchanged.

Stable:
  ICU 4.0

Implements Format.

void PluralFormat::setLocale | ( | const Locale & | locale,
|  | UErrorCode & | status |
| ) |  |  |

Sets the locale used by this `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(const Locale& locale, UErrorCode& status).

Parameters:

  | locale | the `locale` to use to configure the formatter.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

void PluralFormat::setNumberFormat | ( | const NumberFormat * | format,
|  | UErrorCode & | status |
| ) |  |  |

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.
  | status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 4.0

UnicodeString& PluralFormat::toPattern | ( | UnicodeString & | appendTo | ) |

Returns the pattern from applyPattern() or constructor().

Parameters:

  | appendTo | output parameter to receive result. Result is appended to existing contents.

Returns:
  the UnicodeString with inserted pattern.

Stable:
  ICU 4.0

The documentation for this class was generated from the following file:

- plurfmt.h