Logo Search packages:      
Sourcecode: icu version File versions  Download package

PluralFormat Class Reference

#include <plurfmt.h>

Inheritance diagram for PluralFormat:
Collaboration diagram for PluralFormat:

List of all members.

Classes

class  PluralSelector
class  PluralSelectorAdapter

Public Member Functions

void applyPattern (const UnicodeString &pattern, UErrorCode &status)
virtual Formatclone (void) const
UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
UnicodeString format (int32_t number, UErrorCode &status) const
UnicodeString format (double number, UErrorCode &status) const
UnicodeStringformat (int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
UnicodeStringformat (double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
virtual UClassID getDynamicClassID () const
Locale getLocale (ULocDataLocaleType type, UErrorCode &status) const
const char * getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
virtual UBool operator!= (const Format &other) const
PluralFormatoperator= (const PluralFormat &other)
virtual UBool operator== (const Format &other) const
void parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
virtual void parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
 PluralFormat (UErrorCode &status)
 PluralFormat (const Locale &locale, UErrorCode &status)
 PluralFormat (const PluralRules &rules, UErrorCode &status)
 PluralFormat (const Locale &locale, const PluralRules &rules, UErrorCode &status)
 PluralFormat (const UnicodeString &pattern, UErrorCode &status)
 PluralFormat (const Locale &locale, const UnicodeString &pattern, UErrorCode &status)
 PluralFormat (const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
 PluralFormat (const Locale &locale, const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
 PluralFormat (const PluralFormat &other)
void setLocale (const Locale &locale, UErrorCode &status)
void setNumberFormat (const NumberFormat *format, UErrorCode &status)
UnicodeStringtoPattern (UnicodeString &appendTo)
virtual ~PluralFormat ()

Static Public Member Functions

static UClassID U_EXPORT2 getStaticClassID (void)
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Protected Member Functions

void setLocaleIDs (const char *valid, const char *actual)

Static Protected Member Functions

static void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Private Member Functions

void copyObjects (const PluralFormat &other)
void init (const PluralRules *rules, UErrorCode &status)

Static Private Member Functions

static int32_t findSubMessage (const MessagePattern &pattern, int32_t partIndex, const PluralSelector &selector, double number, UErrorCode &ec)

Private Attributes

Locale locale
MessagePattern msgPattern
NumberFormatnumberFormat
double offset
PluralSelectorAdapter pluralRulesWrapper

Friends

class MessageFormat

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 http://unicode.org/repos/cldr-tmp/trunk/diff/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 ICU 4.0

Definition at line 147 of file plurfmt.h.


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

Generated by  Doxygen 1.6.0   Back to index