ICU 74.2: icu::PluralFormat Class Reference

ICU 74.2 74.2

Data Structures | Public Member Functions | Static Public Member Functions | Friends
icu::PluralFormat Class Reference

`#include <plurfmt.h>`

Inheritance diagram for icu::PluralFormat:

## Public Member Functions
| PluralFormat (UErrorCode &status)
| Creates a new cardinal-number `PluralFormat` for the default locale.
| PluralFormat (const Locale &locale, UErrorCode &status)
| Creates a new cardinal-number `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 Locale &locale, UPluralType type, UErrorCode &status)
| Creates a new `PluralFormat` for the plural type.
| PluralFormat (const UnicodeString &pattern, UErrorCode &status)
| Creates a new cardinal-number `PluralFormat` for a given pattern string.
| PluralFormat (const Locale &locale, const UnicodeString &pattern, UErrorCode &status)
| Creates a new cardinal-number `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 Locale &locale, UPluralType type, const UnicodeString &pattern, UErrorCode &status)
| Creates a new `PluralFormat` for a plural type, 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 bool | operator== (const Format &other) const override
| Return true if another object is semantically equal to this one.
virtual bool | operator!= (const Format &other) const
| Return true if another object is semantically unequal to this one.
virtual PluralFormat * | clone () const override
| Clones this Format object polymorphically.
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
| Formats a plural message for a number taken from a Formattable object.
UnicodeString & | toPattern (UnicodeString &appendTo)
| Returns the pattern from applyPattern() or constructor().
virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
| This method is not yet supported by `PluralFormat`.
virtual UClassID | getDynamicClassID () const override
| ICU "poor man's RTTI", returns a UClassID for the actual class.
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
| Formats an object to produce a string.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const=0
| Format an object to produce a string.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format an object to produce a string.
Public Member Functions inherited from icu::Format
virtual | ~Format ()
| Destructor.
virtual bool | operator== (const Format &other) const =0
| Return true if the given Format objects are semantically equal.
bool | operator!= (const Format &other) const
| Return true if the given Format objects are not semantically equal.
virtual Format * | clone () const =0
| Clone this object polymorphically.
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
| Formats an object to produce a string.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const =0
| Format an object to produce a string.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format an object to produce a string.
virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
| Parse a string to produce an object.
void | parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
| Parses a string to produce an object.
Locale | getLocale (ULocDataLocaleType type, UErrorCode &status) const
| Get the locale for this format object.
const char * | getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
| Get the locale for this format object.
Public Member Functions inherited from icu::UObject
virtual | ~UObject ()
| Destructor.
virtual UClassID | getDynamicClassID () const
| ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.

## Static Public Member Functions
static UClassID | getStaticClassID (void)
| ICU "poor man's RTTI", returns a UClassID for this class.

## Friends
class | MessageFormat
class | NFRule

## Additional Inherited Members
Protected Member Functions inherited from icu::Format
void | setLocaleIDs (const char *valid, const char *actual)
| Format ()
| Default constructor for subclass use only.
| Format (const Format &)
Format & | operator= (const Format &)
Static Protected Member Functions inherited from icu::Format
static void | syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)
| Simple function for initializing a UParseError from a UnicodeString.

## 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 user's 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., the plural 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:

- 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 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 https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html

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

#### 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 148 of file plurfmt.h.

## Constructor & Destructor Documentation

## ◆ PluralFormat() [1/11]

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

Creates a new cardinal-number `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() [2/11]

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

Creates a new cardinal-number `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() [3/11]

icu::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() [4/11]

icu::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

#### Sample code

#include <iostream>
#include "unicode/plurfmt.h"
#include "unicode/msgfmt.h"
#include "unicode/ustdio.h"
msgfmt.h
C++ API: Formats messages in a language-neutral way.
plurfmt.h
C++ API: PluralFormat object.
ustdio.h
C API: Unicode stdio-like API.
UErrorCode status =U_ZERO_ERROR;
Locale locEn = Locale("en");
Locale locSl = Locale("sl");
UnicodeString patEn = UnicodeString("one{dog} other{dogs}"); // English 'dog'
UnicodeString patSl = UnicodeString("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 = PluralFormat(locEn, patEn,status);
PluralFormat plfmtSl = PluralFormat(locSl, patSl,status);
// Constructs a MessageFormat for given pattern and locale.
MessageFormat* msgfmtEn = new MessageFormat("{0,number} {1}", locEn,status);
MessageFormat* msgfmtSl = new MessageFormat("{0,number} {1}", locSl,status);
int numbers[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102};
u_printf("Output by using PluralFormat and MessageFormat API\n");
u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
// Use MessageFormat.format () to format the objects and append to the given StringBuffer
for (int i=0;i<sizeof(numbers)/sizeof(int);i++) {
UnicodeString msgEn,msgSl;
FieldPosition fpos = 0;
Formattable argEn[]={Formattable(numbers[i]), Formattable(plfmtEn.format(numbers[i],status))};
Formattable argSl[]={Formattable(numbers[i]), Formattable(plfmtSl.format(numbers[i],status))};
msgfmtEn->format(argEn,2,msgEn,fpos,status);
msgfmtSl->format(argSl,2,msgSl,fpos,status);
u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(),msgSl.getTerminatedBuffer());
}
u_printf("\n");
// Equivalent code with message format pattern
UnicodeString msgPatEn = "{0,plural, one{# dog} other{# dogs}}";
UnicodeString msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}";
MessageFormat* altMsgfmtEn = new MessageFormat(msgPatEn, locEn,status);
MessageFormat* altMsgfmtSl = new MessageFormat(msgPatSl, locSl,status);
u_printf("Same Output by using MessageFormat API only\n");
u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
for (int i=0;i<sizeof(numbers)/sizeof(int);i++) {
UnicodeString msgEn,msgSl;
Formattable arg[] = {numbers[i]};
FieldPosition fPos =0;
altMsgfmtEn->format(arg, 1, msgEn, fPos, status);
altMsgfmtSl->format(arg, 1, msgSl, fPos,status);
u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(), msgSl.getTerminatedBuffer());
}
delete msgfmtEn;
delete msgfmtSl;
delete altMsgfmtEn;
delete altMsgfmtSl;
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:110
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:64
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:195
icu::MessageFormat
Definition msgfmt.h:345
icu::MessageFormat::format
UnicodeString & format(const Formattable *source, int32_t count, UnicodeString &appendTo, FieldPosition &ignore, UErrorCode &status) const
Formats the given array of arguments into a user-readable string.
icu::PluralFormat
Definition plurfmt.h:148
icu::PluralFormat::format
UnicodeString format(int32_t number, UErrorCode &status) const
Formats a plural message for a given number.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
icu::UnicodeString::getTerminatedBuffer
const char16_t * getTerminatedBuffer()
Get a read-only pointer to the internal buffer, making sure that it is NUL-terminated.
u_printf
U_CAPI int32_t u_printf(const char *patternSpecification,...)
Write formatted data to stdout.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:415
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:449

## ◆ PluralFormat() [5/11]

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

Creates a new `PluralFormat` for the plural type.

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

Parameters

  locale | the default number formatting will be done using this locale.
  type | The plural type (e.g., cardinal or ordinal).
  status | output param set to success/failure code on exit, which must not indicate a failure before the function call.

Stable:
  ICU 50

## ◆ PluralFormat() [6/11]

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

Creates a new cardinal-number `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() [7/11]

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

Creates a new cardinal-number `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() [8/11]

icu::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() [9/11]

icu::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() [10/11]

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

Creates a new `PluralFormat` for a plural type, 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.
  type | The plural type (e.g., cardinal or ordinal).
  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 50

## ◆ PluralFormat() [11/11]

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

copy constructor.

Stable:
  ICU 4.0

## ◆ ~PluralFormat()

virtual icu::PluralFormat::~PluralFormat | ( |  | ) |
| virtual

Destructor.

Stable:
  ICU 4.0

## Member Function Documentation

## ◆ applyPattern()

void icu::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

## ◆ clone()

virtual PluralFormat * icu::PluralFormat::clone | ( |  | ) | const
| overridevirtual

Clones this Format object polymorphically.

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

Stable:
  ICU 4.0

Implements icu::Format.

## ◆ format() [1/8]

UnicodeString & icu::PluralFormat::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| overridevirtual

Formats a plural message for a number taken from a Formattable object.

Parameters

  obj | The object containing a number for which the plural message should be formatted. The object must be of a numeric type.
  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 icu::Format.

## ◆ format() [2/8]

virtual UnicodeString & icu::Format::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an object to produce a string.

This is a pure virtual method which subclasses must implement. This method allows polymorphic formatting of Formattable objects. If a subclass of Format receives a Formattable object type it doesn't handle (e.g., if a numeric Formattable is passed to a DateFormat object) then it returns a failing UErrorCode.

Parameters

  obj | The object to format.
  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 2.0

Implements icu::Format.

## ◆ format() [3/8]

virtual UnicodeString & icu::Format::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an object to produce a string.

Subclasses should override this method. This method allows polymorphic formatting of Formattable objects. If a subclass of Format receives a Formattable object type it doesn't handle (e.g., if a numeric Formattable is passed to a DateFormat object) then it returns a failing UErrorCode.

Parameters

  obj | The object to format.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 4.4

Reimplemented from icu::Format.

## ◆ format() [4/8]

UnicodeString & icu::Format::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | UErrorCode & | status
| ) |  | const

Formats an object to produce a string.

Parameters

  obj | The object to format.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  status | Output parameter filled in with success or failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

## ◆ format() [5/8]

UnicodeString icu::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

## ◆ format() [6/8]

UnicodeString & icu::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

## ◆ format() [7/8]

UnicodeString icu::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

## ◆ format() [8/8]

UnicodeString & icu::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

## ◆ getDynamicClassID()

virtual UClassID icu::PluralFormat::getDynamicClassID | ( |  | ) | const
| overridevirtual

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

Stable:
  ICU 4.0

Reimplemented from icu::UObject.

## ◆ getStaticClassID()

static UClassID icu::PluralFormat::getStaticClassID | ( | void |  | ) |
| static

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

Stable:
  ICU 4.0

## ◆ operator!=()

virtual bool icu::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

## ◆ operator=()

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

Assignment operator.

Parameters

  other | the PluralFormat object to copy from.

Stable:
  ICU 4.0

## ◆ operator==()

virtual bool icu::PluralFormat::operator== | ( | const Format & | other | ) | const
| overridevirtual

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 icu::Format.

## ◆ parseObject()

virtual void icu::PluralFormat::parseObject | ( | const UnicodeString & | source,
|  | Formattable & | result,
|  | ParsePosition & | parse_pos
| ) |  | const
| overridevirtual

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 icu::Format.

## ◆ setLocale()

void icu::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, UPluralType type, UErrorCode& status)` with UPLURAL_TYPE_CARDINAL.

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.

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.

## ◆ setNumberFormat()

void icu::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

## ◆ toPattern()

UnicodeString & icu::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

## Friends And Related Symbol Documentation

## ◆ MessageFormat

friend class MessageFormat
| friend

Definition at line 595 of file plurfmt.h.

## ◆ NFRule

friend class NFRule
| friend

Definition at line 596 of file plurfmt.h.

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

- i18n/unicode/plurfmt.h