michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2007-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: michael@0: * File PLURFMT.H michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #ifndef PLURFMT michael@0: #define PLURFMT michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: PluralFormat object michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/messagepattern.h" michael@0: #include "unicode/numfmt.h" michael@0: #include "unicode/plurrule.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class Hashtable; michael@0: michael@0: /** michael@0: *
michael@0: * PluralFormat
supports the creation of internationalized
michael@0: * messages with plural inflection. It is based on plural
michael@0: * selection, i.e. the caller specifies messages for each
michael@0: * plural case that can appear in the user's language and the
michael@0: * PluralFormat
selects the appropriate message based on
michael@0: * the number.
michael@0: *
michael@0: * Different languages have different ways to inflect
michael@0: * plurals. Creating internationalized messages that include plural
michael@0: * forms is only feasible when the framework is able to handle plural
michael@0: * forms of all languages correctly. ChoiceFormat
michael@0: * doesn't handle this well, because it attaches a number interval to
michael@0: * each message and selects the message whose interval contains a
michael@0: * given number. This can only handle a finite number of
michael@0: * intervals. But in some languages, like Polish, one plural case
michael@0: * applies to infinitely many intervals (e.g., the plural case applies to
michael@0: * numbers ending with 2, 3, or 4 except those ending with 12, 13, or
michael@0: * 14). Thus ChoiceFormat
is not adequate.
michael@0: *
michael@0: * PluralFormat
deals with this by breaking the problem
michael@0: * into two parts:
michael@0: *
PluralRules
that can define more complex
michael@0: * conditions for a plural case than just a single interval. These plural
michael@0: * rules define both what plural cases exist in a language, and to
michael@0: * which numbers these cases apply.
michael@0: * PluralFormat
Note: Typically, plural formatting is done via MessageFormat
michael@0: * with a plural
argument type,
michael@0: * rather than using a stand-alone PluralFormat
.
michael@0: *
michael@0: * This discussion assumes that you use PluralFormat
with
michael@0: * a predefined set of plural rules. You can create one using one of
michael@0: * the constructors that takes a locale
object. To
michael@0: * specify the message pattern, you can either pass it to the
michael@0: * constructor or set it explicitly using the
michael@0: * applyPattern()
method. The format()
michael@0: * method takes a number object and selects the message of the
michael@0: * matching plural case. This message will be returned.
michael@0: *
michael@0: * The pattern text defines the message output for each plural case of the michael@0: * specified locale. Syntax: michael@0: *
michael@0: * pluralStyle = [offsetValue] (selector '{' message '}')+ michael@0: * offsetValue = "offset:" number michael@0: * selector = explicitValue | keyword michael@0: * explicitValue = '=' number // adjacent, no white space in between michael@0: * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ michael@0: * message: see {@link MessageFormat} michael@0: *michael@0: * Pattern_White_Space between syntax elements is ignored, except michael@0: * between the {curly braces} and their sub-message, michael@0: * and between the '=' and the number of an explicitValue. michael@0: * michael@0: *
michael@0: * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and
michael@0: * 'other'. You always have to define a message text for the default plural case
michael@0: * other
which is contained in every rule set.
michael@0: * If you do not specify a message text for a particular plural case, the
michael@0: * message text of the plural case other
gets assigned to this
michael@0: * plural case.
michael@0: *
michael@0: * When formatting, the input number is first matched against the explicitValue clauses.
michael@0: * If there is no exact-number match, then a keyword is selected by calling
michael@0: * the PluralRules
with the input number minus the offset.
michael@0: * (The offset defaults to 0 if it is omitted from the pattern string.)
michael@0: * If there is no clause with that keyword, then the "other" clauses is returned.
michael@0: *
michael@0: * An unquoted pound sign (#
) in the selected sub-message
michael@0: * itself (i.e., outside of arguments nested in the sub-message)
michael@0: * is replaced by the input number minus the offset.
michael@0: * The number-minus-offset value is formatted using a
michael@0: * NumberFormat
for the PluralFormat
's locale. If you
michael@0: * need special number formatting, you have to use a MessageFormat
michael@0: * and explicitly specify a NumberFormat
argument.
michael@0: * Note: That argument is formatting without subtracting the offset!
michael@0: * If you need a custom format and have a non-zero offset, then you need to pass the
michael@0: * number-minus-offset value as a separate parameter.
michael@0: *
If you need to use PluralFormat
with custom rules, you can
michael@0: * create a PluralRules
object and pass it to
michael@0: * PluralFormat
's constructor. If you also specify a locale in this
michael@0: * constructor, this locale will be used to format the number in the message
michael@0: * texts.
michael@0: *
michael@0: * For more information about PluralRules
, see
michael@0: * {@link PluralRules}.
michael@0: *
PluralFormat
for the default locale.
michael@0: * This locale will be used to get the set of plural rules and for standard
michael@0: * number formatting.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new cardinal-number PluralFormat
for a given locale.
michael@0: * @param locale the PluralFormat
will be configured with
michael@0: * rules for this locale. This locale will also be used for
michael@0: * standard number formatting.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const Locale& locale, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for a given set of rules.
michael@0: * The standard number formatting will be done using the default locale.
michael@0: * @param rules defines the behavior of the PluralFormat
michael@0: * object.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const PluralRules& rules, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for a given set of rules.
michael@0: * The standard number formatting will be done using the given locale.
michael@0: * @param locale the default number formatting will be done using this
michael@0: * locale.
michael@0: * @param rules defines the behavior of the PluralFormat
michael@0: * object.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: * michael@0: *
michael@0: */
michael@0: PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for the plural type.
michael@0: * The standard number formatting will be done using the given locale.
michael@0: * @param locale the default number formatting will be done using this
michael@0: * locale.
michael@0: * @param type The plural type (e.g., cardinal or ordinal).
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 50
michael@0: */
michael@0: PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new cardinal-number PluralFormat
for a given pattern string.
michael@0: * The default locale will be used to get the set of plural rules and for
michael@0: * standard number formatting.
michael@0: * @param pattern the pattern for this PluralFormat
.
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const UnicodeString& pattern, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new cardinal-number PluralFormat
for a given pattern string and
michael@0: * locale.
michael@0: * The locale will be used to get the set of plural rules and for
michael@0: * standard number formatting.
michael@0: * @param locale the PluralFormat
will be configured with
michael@0: * rules for this locale. This locale will also be used for
michael@0: * standard number formatting.
michael@0: * @param pattern the pattern for this PluralFormat
.
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for a given set of rules, a
michael@0: * pattern and a locale.
michael@0: * @param rules defines the behavior of the PluralFormat
michael@0: * object.
michael@0: * @param pattern the pattern for this PluralFormat
.
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const PluralRules& rules,
michael@0: const UnicodeString& pattern,
michael@0: UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for a given set of rules, a
michael@0: * pattern and a locale.
michael@0: * @param locale the PluralFormat
will be configured with
michael@0: * rules for this locale. This locale will also be used for
michael@0: * standard number formatting.
michael@0: * @param rules defines the behavior of the PluralFormat
michael@0: * object.
michael@0: * @param pattern the pattern for this PluralFormat
.
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const Locale& locale,
michael@0: const PluralRules& rules,
michael@0: const UnicodeString& pattern,
michael@0: UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Creates a new PluralFormat
for a plural type, a
michael@0: * pattern and a locale.
michael@0: * @param locale the PluralFormat
will be configured with
michael@0: * rules for this locale. This locale will also be used for
michael@0: * standard number formatting.
michael@0: * @param type The plural type (e.g., cardinal or ordinal).
michael@0: * @param pattern the pattern for this PluralFormat
.
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 50
michael@0: */
michael@0: PluralFormat(const Locale& locale,
michael@0: UPluralType type,
michael@0: const UnicodeString& pattern,
michael@0: UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * copy constructor.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat(const PluralFormat& other);
michael@0:
michael@0: /**
michael@0: * Destructor.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: virtual ~PluralFormat();
michael@0:
michael@0: /**
michael@0: * Sets the pattern used by this plural format.
michael@0: * The method parses the pattern and creates a map of format strings
michael@0: * for the plural rules.
michael@0: * Patterns and their interpretation are specified in the class description.
michael@0: *
michael@0: * @param pattern the pattern for this plural format
michael@0: * errors are returned to status if the pattern is invalid.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: void applyPattern(const UnicodeString& pattern, UErrorCode& status);
michael@0:
michael@0:
michael@0: using Format::format;
michael@0:
michael@0: /**
michael@0: * Formats a plural message for a given number.
michael@0: *
michael@0: * @param number a number for which the plural message should be formatted
michael@0: * for. If no pattern has been applied to this
michael@0: * PluralFormat
object yet, the formatted number
michael@0: * will be returned.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @return the string containing the formatted plural message.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString format(int32_t number, UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Formats a plural message for a given number.
michael@0: *
michael@0: * @param number a number for which the plural message should be formatted
michael@0: * for. If no pattern has been applied to this
michael@0: * PluralFormat object yet, the formatted number
michael@0: * will be returned.
michael@0: * @param status output param set to success or failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @return the string containing the formatted plural message.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString format(double number, UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Formats a plural message for a given number.
michael@0: *
michael@0: * @param number a number for which the plural message should be formatted
michael@0: * for. If no pattern has been applied to this
michael@0: * PluralFormat
object yet, the formatted number
michael@0: * will be returned.
michael@0: * @param appendTo output parameter to receive result.
michael@0: * result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @return the string containing the formatted plural message.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString& format(int32_t number,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos,
michael@0: UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Formats a plural message for a given number.
michael@0: *
michael@0: * @param number a number for which the plural message should be formatted
michael@0: * for. If no pattern has been applied to this
michael@0: * PluralFormat object yet, the formatted number
michael@0: * will be returned.
michael@0: * @param appendTo output parameter to receive result.
michael@0: * result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @return the string containing the formatted plural message.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString& format(double number,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos,
michael@0: UErrorCode& status) const;
michael@0:
michael@0: #ifndef U_HIDE_DEPRECATED_API
michael@0: /**
michael@0: * Sets the locale used by this PluraFormat
object.
michael@0: * Note: Calling this method resets this PluraFormat
object,
michael@0: * i.e., a pattern that was applied previously will be removed,
michael@0: * and the NumberFormat is set to the default number format for
michael@0: * the locale. The resulting format behaves the same as one
michael@0: * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)}
michael@0: * with UPLURAL_TYPE_CARDINAL.
michael@0: * @param locale the locale
to use to configure the formatter.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @deprecated ICU 50 This method clears the pattern and might create
michael@0: * a different kind of PluralRules instance;
michael@0: * use one of the constructors to create a new instance instead.
michael@0: */
michael@0: void setLocale(const Locale& locale, UErrorCode& status);
michael@0: #endif /* U_HIDE_DEPRECATED_API */
michael@0:
michael@0: /**
michael@0: * Sets the number format used by this formatter. You only need to
michael@0: * call this if you want a different number format than the default
michael@0: * formatter for the locale.
michael@0: * @param format the number format to use.
michael@0: * @param status output param set to success/failure code on exit, which
michael@0: * must not indicate a failure before the function call.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: void setNumberFormat(const NumberFormat* format, UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Assignment operator
michael@0: *
michael@0: * @param other the PluralFormat object to copy from.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: PluralFormat& operator=(const PluralFormat& other);
michael@0:
michael@0: /**
michael@0: * Return true if another object is semantically equal to this one.
michael@0: *
michael@0: * @param other the PluralFormat object to be compared with.
michael@0: * @return true if other is semantically equal to this.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: virtual UBool operator==(const Format& other) const;
michael@0:
michael@0: /**
michael@0: * Return true if another object is semantically unequal to this one.
michael@0: *
michael@0: * @param other the PluralFormat object to be compared with.
michael@0: * @return true if other is semantically unequal to this.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: virtual UBool operator!=(const Format& other) const;
michael@0:
michael@0: /**
michael@0: * Clones this Format object polymorphically. The caller owns the
michael@0: * result and should delete it when done.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: virtual Format* clone(void) const;
michael@0:
michael@0: /**
michael@0: * Formats a plural message for a number taken from a Formattable object.
michael@0: *
michael@0: * @param obj The object containing a number for which the
michael@0: * plural message should be formatted.
michael@0: * The object must be of a numeric type.
michael@0: * @param appendTo output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @param status output param filled with success/failure status.
michael@0: * @return Reference to 'appendTo' parameter.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString& format(const Formattable& obj,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos,
michael@0: UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Returns the pattern from applyPattern() or constructor().
michael@0: *
michael@0: * @param appendTo output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @return the UnicodeString with inserted pattern.
michael@0: * @stable ICU 4.0
michael@0: */
michael@0: UnicodeString& toPattern(UnicodeString& appendTo);
michael@0:
michael@0: /**
michael@0: * This method is not yet supported by PluralFormat
.
michael@0: *
michael@0: * Before calling, set parse_pos.index to the offset you want to start michael@0: * parsing at in the source. After calling, parse_pos.index is the end of michael@0: * the text you parsed. If error occurs, index is unchanged. michael@0: *
michael@0: * When parsing, leading whitespace is discarded (with a successful parse), michael@0: * while trailing whitespace is left as is. michael@0: *
michael@0: * See Format::parseObject() for more. michael@0: * michael@0: * @param source The string to be parsed into an object. michael@0: * @param result Formattable to be set to the parse result. michael@0: * If parse fails, return contents are undefined. michael@0: * @param parse_pos The position to start parsing at. Upon return michael@0: * this param is set to the position after the michael@0: * last character successfully parsed. If the michael@0: * source is not parsed successfully, this param michael@0: * will remain unchanged. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual void parseObject(const UnicodeString& source, michael@0: Formattable& result, michael@0: ParsePosition& parse_pos) const; michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for this class. michael@0: * michael@0: * @stable ICU 4.0 michael@0: * michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for the actual class. michael@0: * michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: #if (defined(__xlC__) && (__xlC__ < 0x0C00)) || (U_PLATFORM == U_PF_OS390) || (U_PLATFORM ==U_PF_OS400) michael@0: // Work around a compiler bug on xlC 11.1 on AIX 7.1 that would michael@0: // prevent PluralSelectorAdapter from implementing private PluralSelector. michael@0: // xlC error message: michael@0: // 1540-0300 (S) The "private" member "class icu_49::PluralFormat::PluralSelector" cannot be accessed. michael@0: public: michael@0: #else michael@0: private: michael@0: #endif michael@0: /** michael@0: * @internal michael@0: */ michael@0: class U_I18N_API PluralSelector : public UMemory { michael@0: public: michael@0: virtual ~PluralSelector(); michael@0: /** michael@0: * Given a number, returns the appropriate PluralFormat keyword. michael@0: * michael@0: * @param context worker object for the selector. michael@0: * @param number The number to be plural-formatted. michael@0: * @param ec Error code. michael@0: * @return The selected PluralFormat keyword. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0; michael@0: }; michael@0: michael@0: /** michael@0: * @internal michael@0: */ michael@0: class U_I18N_API PluralSelectorAdapter : public PluralSelector { michael@0: public: michael@0: PluralSelectorAdapter() : pluralRules(NULL) { michael@0: } michael@0: michael@0: virtual ~PluralSelectorAdapter(); michael@0: michael@0: virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const; /**< @internal */ michael@0: michael@0: void reset(); michael@0: michael@0: PluralRules* pluralRules; michael@0: }; michael@0: michael@0: #if defined(__xlC__) michael@0: // End of xlC bug workaround, keep remaining definitions private. michael@0: private: michael@0: #endif michael@0: Locale locale; michael@0: MessagePattern msgPattern; michael@0: NumberFormat* numberFormat; michael@0: double offset; michael@0: PluralSelectorAdapter pluralRulesWrapper; michael@0: michael@0: PluralFormat(); // default constructor not implemented michael@0: void init(const PluralRules* rules, UPluralType type, UErrorCode& status); michael@0: /** michael@0: * Copies dynamically allocated values (pointer fields). michael@0: * Others are copied using their copy constructors and assignment operators. michael@0: */ michael@0: void copyObjects(const PluralFormat& other); michael@0: michael@0: UnicodeString& format(const Formattable& numberObject, double number, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; /**< @internal */ michael@0: michael@0: /** michael@0: * Finds the PluralFormat sub-message for the given number, or the "other" sub-message. michael@0: * @param pattern A MessagePattern. michael@0: * @param partIndex the index of the first PluralFormat argument style part. michael@0: * @param selector the PluralSelector for mapping the number (minus offset) to a keyword. michael@0: * @param context worker object for the selector. michael@0: * @param number a number to be matched to one of the PluralFormat argument's explicit values, michael@0: * or mapped via the PluralSelector. michael@0: * @param ec ICU error code. michael@0: * @return the sub-message start part index. michael@0: */ michael@0: static int32_t findSubMessage( michael@0: const MessagePattern& pattern, int32_t partIndex, michael@0: const PluralSelector& selector, void *context, double number, UErrorCode& ec); /**< @internal */ michael@0: michael@0: friend class MessageFormat; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // _PLURFMT michael@0: //eof