Movatterモバイル変換


[0]ホーム

URL:


ICU 78.1  78.1
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

U_I18N_API PluralFormat (UErrorCode &status)
 Creates a new cardinal-numberPluralFormat for the default locale.More...
 
U_I18N_API PluralFormat (constLocale &locale,UErrorCode &status)
 Creates a new cardinal-numberPluralFormat for a given locale.More...
 
U_I18N_API PluralFormat (constPluralRules &rules,UErrorCode &status)
 Creates a newPluralFormat for a given set of rules.More...
 
U_I18N_API PluralFormat (constLocale &locale, constPluralRules &rules,UErrorCode &status)
 Creates a newPluralFormat for a given set of rules.More...
 
U_I18N_API PluralFormat (constLocale &locale,UPluralType type,UErrorCode &status)
 Creates a newPluralFormat for the plural type.More...
 
U_I18N_API PluralFormat (constUnicodeString &pattern,UErrorCode &status)
 Creates a new cardinal-numberPluralFormat for a given pattern string.More...
 
U_I18N_API PluralFormat (constLocale &locale, constUnicodeString &pattern,UErrorCode &status)
 Creates a new cardinal-numberPluralFormat for a given pattern string and locale.More...
 
U_I18N_API PluralFormat (constPluralRules &rules, constUnicodeString &pattern,UErrorCode &status)
 Creates a newPluralFormat for a given set of rules, a pattern and a locale.More...
 
U_I18N_API PluralFormat (constLocale &locale, constPluralRules &rules, constUnicodeString &pattern,UErrorCode &status)
 Creates a newPluralFormat for a given set of rules, a pattern and a locale.More...
 
U_I18N_API PluralFormat (constLocale &locale,UPluralType type, constUnicodeString &pattern,UErrorCode &status)
 Creates a newPluralFormat for a plural type, a pattern and a locale.More...
 
U_I18N_API PluralFormat (constPluralFormat &other)
 copy constructor.More...
 
virtualU_I18N_API ~PluralFormat ()
 Destructor.More...
 
U_I18N_API void applyPattern (constUnicodeString &pattern,UErrorCode &status)
 Sets the pattern used by this plural format.More...
 
U_I18N_APIUnicodeString format (int32_t number,UErrorCode &status) const
 Formats a plural message for a given number.More...
 
U_I18N_APIUnicodeString format (double number,UErrorCode &status) const
 Formats a plural message for a given number.More...
 
U_I18N_APIUnicodeStringformat (int32_t number,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const
 Formats a plural message for a given number.More...
 
U_I18N_APIUnicodeStringformat (double number,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const
 Formats a plural message for a given number.More...
 
U_I18N_API void setLocale (constLocale &locale,UErrorCode &status)
 Sets the locale used by thisPluraFormat object.More...
 
U_I18N_API void setNumberFormat (constNumberFormat *format,UErrorCode &status)
 Sets the number format used by this formatter.More...
 
U_I18N_APIPluralFormatoperator= (constPluralFormat &other)
 Assignment operator.More...
 
virtualU_I18N_API bool operator== (constFormat &other) const override
 Return true if another object is semantically equal to this one.More...
 
virtualU_I18N_API bool operator!= (constFormat &other) const
 Return true if another object is semantically unequal to this one.More...
 
virtualU_I18N_APIPluralFormatclone () const override
 Clones thisFormat object polymorphically.More...
 
U_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const override
 Formats a plural message for a number taken from aFormattable object.More...
 
U_I18N_APIUnicodeStringtoPattern (UnicodeString &appendTo)
 Returns the pattern fromapplyPattern() or constructor().More...
 
virtualU_I18N_API void parseObject (constUnicodeString &source,Formattable &result,ParsePosition &parse_pos) const override
 This method is not yet supported byPluralFormat.More...
 
virtualU_I18N_APIUClassID getDynamicClassID () const override
 ICU "poor man's RTTI", returns a UClassID for the actual class.More...
 
U_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const
 Formats an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const=0
 Format an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const
 Format an object to produce a string.More...
 
- Public Member Functions inherited fromicu::Format
virtualU_I18N_API ~Format ()
 Destructor.More...
 
U_I18N_API bool operator!= (constFormat &other) const
 Return true if the givenFormat objects are not semantically equal.More...
 
U_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const
 Formats an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const
 Format an object to produce a string.More...
 
U_I18N_API void parseObject (constUnicodeString &source,Formattable &result,UErrorCode &status) const
 Parses a string to produce an object.More...
 
U_I18N_APILocale getLocale (ULocDataLocaleType type,UErrorCode &status) const
 Get the locale for this format object.More...
 
U_I18N_API const char * getLocaleID (ULocDataLocaleType type,UErrorCode &status) const
 Get the locale for this format object.More...
 
- Public Member Functions inherited fromicu::UObject
virtual ~UObject ()
 Destructor.More...
 

Static Public Member Functions

staticU_I18N_APIUClassID getStaticClassID ()
 ICU "poor man's RTTI", returns a UClassID for this class.More...
 

Friends

class MessageFormat
 
class NFRule
 

Additional Inherited Members

- Protected Member Functions inherited fromicu::Format
U_I18N_API void setLocaleIDs (const char *valid, const char *actual)
 
U_I18N_API Format ()
 Default constructor for subclass use only.More...
 
U_I18N_API Format (constFormat &)
 
U_I18N_APIFormatoperator= (constFormat &)
 
- Static Protected Member Functions inherited fromicu::Format
staticU_I18N_API void syntaxError (constUnicodeString &pattern, int32_t pos,UParseError &parseError)
 Simple function for initializing aUParseError from aUnicodeString.More...
 

Detailed Description

PluralFormat supports the creation of internationalized messages with plural inflection. It is based onplural selection, i.e. the caller specifies messages for each plural case that can appear in the user's language and thePluralFormat 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 ofall 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). ThusChoiceFormat is not adequate.

PluralFormat deals with this by breaking the problem into two parts:

Usage ofPluralFormat

Note: Typically, plural formatting is done viaMessageFormat with aplural argument type, rather than using a stand-alonePluralFormat.

This discussion assumes that you usePluralFormat with a predefined set of plural rules. You can create one using one of the constructors that takes alocale object. 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:

pluralStyle = [offsetValue] (selector '{' message '}')+offsetValue = "offset:" numberselector = explicitValue | keywordexplicitValue = '=' number  // adjacent, no white space in betweenkeyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+message: seeMessageFormat

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 caseother 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 caseother 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 thePluralRules with the input numberminus 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 aNumberFormat for thePluralFormat's locale. If you need special number formatting, you have to use aMessageFormat and explicitly specify aNumberFormat 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 theMessageFormat class documentation.

Defining Custom Plural Rules

If you need to usePluralFormat with custom rules, you can create aPluralRules object 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 aboutPluralRules, seePluralRules.

ported from Java

Stable:
ICU 4.0

Definition at line148 of fileplurfmt.h.

Constructor & Destructor Documentation

◆ PluralFormat()[1/11]

U_I18N_API icu::PluralFormat::PluralFormat(UErrorCodestatus)

Creates a new cardinal-numberPluralFormat for the default locale.

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

Parameters
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
UErrorCodestatus 
)

Creates a new cardinal-numberPluralFormat for a given locale.

Parameters
localethePluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constPluralRulesrules,
UErrorCodestatus 
)

Creates a newPluralFormat for a given set of rules.

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

Parameters
rulesdefines the behavior of thePluralFormat object.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
constPluralRulesrules,
UErrorCodestatus 
)

Creates a newPluralFormat for a given set of rules.

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

Parameters
localethe default number formatting will be done using this locale.
rulesdefines the behavior of thePluralFormat object.
statusoutput param set to success/failure code on exit, which must not indicate a failure before the function call.
Stable:
ICU 4.0

◆ PluralFormat()[5/11]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
UPluralType type,
UErrorCodestatus 
)

Creates a newPluralFormat for the plural type.

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

Parameters
localethe default number formatting will be done using this locale.
typeThe plural type (e.g., cardinal or ordinal).
statusoutput param set to success/failure code on exit, which must not indicate a failure before the function call.
Stable:
ICU 50

◆ PluralFormat()[6/11]

U_I18N_API icu::PluralFormat::PluralFormat(constUnicodeStringpattern,
UErrorCodestatus 
)

Creates a new cardinal-numberPluralFormat for a given pattern string.

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

Parameters
patternthe pattern for thisPluralFormat. errors are returned to status if the pattern is invalid.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
constUnicodeStringpattern,
UErrorCodestatus 
)

Creates a new cardinal-numberPluralFormat 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
localethePluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
patternthe pattern for thisPluralFormat. errors are returned to status if the pattern is invalid.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constPluralRulesrules,
constUnicodeStringpattern,
UErrorCodestatus 
)

Creates a newPluralFormat for a given set of rules, a pattern and a locale.

Parameters
rulesdefines the behavior of thePluralFormat object.
patternthe pattern for thisPluralFormat. errors are returned to status if the pattern is invalid.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
constPluralRulesrules,
constUnicodeStringpattern,
UErrorCodestatus 
)

Creates a newPluralFormat for a given set of rules, a pattern and a locale.

Parameters
localethePluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
rulesdefines the behavior of thePluralFormat object.
patternthe pattern for thisPluralFormat. errors are returned to status if the pattern is invalid.
statusoutput 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]

U_I18N_API icu::PluralFormat::PluralFormat(constLocalelocale,
UPluralType type,
constUnicodeStringpattern,
UErrorCodestatus 
)

Creates a newPluralFormat for a plural type, a pattern and a locale.

Parameters
localethePluralFormat will be configured with rules for this locale. This locale will also be used for standard number formatting.
typeThe plural type (e.g., cardinal or ordinal).
patternthe pattern for thisPluralFormat. errors are returned to status if the pattern is invalid.
statusoutput param set to success/failure code on exit, which must not indicate a failure before the function call.
Stable:
ICU 50

◆ PluralFormat()[11/11]

U_I18N_API icu::PluralFormat::PluralFormat(constPluralFormatother)

copy constructor.

Stable:
ICU 4.0

◆ ~PluralFormat()

virtualU_I18N_API icu::PluralFormat::~PluralFormat()
virtual

Destructor.

Stable:
ICU 4.0

Member Function Documentation

◆ applyPattern()

U_I18N_API void icu::PluralFormat::applyPattern(constUnicodeStringpattern,
UErrorCodestatus 
)

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
patternthe pattern for this plural format errors are returned to status if the pattern is invalid.
statusoutput param set to success/failure code on exit, which must not indicate a failure before the function call.
Stable:
ICU 4.0

◆ clone()

virtualU_I18N_APIPluralFormat* icu::PluralFormat::clone() const
overridevirtual

Clones thisFormat object polymorphically.

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

Stable:
ICU 4.0

Implementsicu::Format.

◆ format()[1/8]

U_I18N_APIUnicodeString& icu::PluralFormat::format(constFormattableobj,
UnicodeStringappendTo,
FieldPositionpos,
UErrorCodestatus 
) const
overridevirtual

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

Parameters
objThe object containing a number for which the plural message should be formatted. The object must be of a numeric type.
appendTooutput parameter to receive result. Result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusoutput param filled with success/failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 4.0

Implementsicu::Format.

◆ format()[2/8]

virtualU_I18N_APIUnicodeString& icu::Format::format

Format an object to produce a string.

This is a pure virtual method which subclasses must implement. This method allows polymorphic formatting ofFormattable objects. If a subclass ofFormat receives aFormattable object type it doesn't handle (e.g., if a numericFormattable is passed to aDateFormat object) then it returns a failing UErrorCode.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusOutput param filled with success/failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[3/8]

virtualU_I18N_APIUnicodeString& icu::Format::format

Format an object to produce a string.

Subclasses should override this method. This method allows polymorphic formatting ofFormattable objects. If a subclass ofFormat receives aFormattable object type it doesn't handle (e.g., if a numericFormattable is passed to aDateFormat object) then it returns a failing UErrorCode.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
posIterOn return, can be used to iterate over positions of fields generated by this format call.
statusOutput param filled with success/failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 4.4

◆ format()[4/8]

U_I18N_APIUnicodeString& icu::Format::format

Formats an object to produce a string.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
statusOutput parameter filled in with success or failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[5/8]

U_I18N_APIUnicodeString icu::PluralFormat::format(double number,
UErrorCodestatus 
) const

Formats a plural message for a given number.

Parameters
numbera number for which the plural message should be formatted for. If no pattern has been applied to thisPluralFormat object yet, the formatted number will be returned.
statusoutput 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]

U_I18N_APIUnicodeString& icu::PluralFormat::format(double number,
UnicodeStringappendTo,
FieldPositionpos,
UErrorCodestatus 
) const

Formats a plural message for a given number.

Parameters
numbera number for which the plural message should be formatted for. If no pattern has been applied to thisPluralFormat object yet, the formatted number will be returned.
appendTooutput parameter to receive result. result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusoutput 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]

U_I18N_APIUnicodeString icu::PluralFormat::format(int32_t number,
UErrorCodestatus 
) const

Formats a plural message for a given number.

Parameters
numbera number for which the plural message should be formatted for. If no pattern has been applied to thisPluralFormat object yet, the formatted number will be returned.
statusoutput 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]

U_I18N_APIUnicodeString& icu::PluralFormat::format(int32_t number,
UnicodeStringappendTo,
FieldPositionpos,
UErrorCodestatus 
) const

Formats a plural message for a given number.

Parameters
numbera number for which the plural message should be formatted for. If no pattern has been applied to thisPluralFormat object yet, the formatted number will be returned.
appendTooutput parameter to receive result. result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusoutput 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()

virtualU_I18N_APIUClassID icu::PluralFormat::getDynamicClassID() const
overridevirtual

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

Stable:
ICU 4.0

Reimplemented fromicu::UObject.

◆ getStaticClassID()

staticU_I18N_APIUClassID icu::PluralFormat::getStaticClassID()
static

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

Stable:
ICU 4.0

◆ operator!=()

virtualU_I18N_API bool icu::PluralFormat::operator!=(constFormatother) const
virtual

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

Parameters
otherthePluralFormat object to be compared with.
Returns
true if other is semantically unequal to this.
Stable:
ICU 4.0

◆ operator=()

U_I18N_APIPluralFormat& icu::PluralFormat::operator=(constPluralFormatother)

Assignment operator.

Parameters
otherthePluralFormat object to copy from.
Stable:
ICU 4.0

◆ operator==()

virtualU_I18N_API bool icu::PluralFormat::operator==(constFormatother) const
overridevirtual

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

Parameters
otherthePluralFormat object to be compared with.
Returns
true if other is semantically equal to this.
Stable:
ICU 4.0

Implementsicu::Format.

◆ parseObject()

virtualU_I18N_API void icu::PluralFormat::parseObject(constUnicodeStringsource,
Formattableresult,
ParsePositionparse_pos 
) const
overridevirtual

This method is not yet supported byPluralFormat.

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.

SeeFormat::parseObject() for more.

Parameters
sourceThe string to be parsed into an object.
resultFormattable to be set to the parse result. If parse fails, return contents are undefined.
parse_posThe 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

Implementsicu::Format.

◆ setLocale()

U_I18N_API void icu::PluralFormat::setLocale(constLocalelocale,
UErrorCodestatus 
)

Sets the locale used by thisPluraFormat object.

Note: Calling this method resets thisPluraFormat object, i.e., a pattern that was applied previously will be removed, and theNumberFormat is set to the default number format for the locale. The resulting format behaves the same as one constructed fromPluralFormat(const Locale& locale, UPluralType type, UErrorCode& status) with UPLURAL_TYPE_CARDINAL.

Parameters
localethelocale to use to configure the formatter.
statusoutput 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 ofPluralRules instance; use one of the constructors to create a new instance instead.

◆ setNumberFormat()

U_I18N_API void icu::PluralFormat::setNumberFormat(constNumberFormatformat,
UErrorCodestatus 
)

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
formatthe number format to use.
statusoutput param set to success/failure code on exit, which must not indicate a failure before the function call.
Stable:
ICU 4.0

◆ toPattern()

U_I18N_APIUnicodeString& icu::PluralFormat::toPattern(UnicodeStringappendTo)

Returns the pattern fromapplyPattern() or constructor().

Parameters
appendTooutput parameter to receive result. Result is appended to existing contents.
Returns
theUnicodeString with inserted pattern.
Stable:
ICU 4.0

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

Generated by doxygen 1.9.1
[8]ページ先頭

©2009-2025 Movatter.jp