2 *******************************************************************************
3 * Copyright (C) 2007-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 ********************************************************************************
15 #include "unicode/utypes.h"
19 * \brief C++ API: PluralFormat object
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/messagepattern.h"
25 #include "unicode/numfmt.h"
26 #include "unicode/plurrule.h"
35 * <code>PluralFormat</code> supports the creation of internationalized
36 * messages with plural inflection. It is based on <i>plural
37 * selection</i>, i.e. the caller specifies messages for each
38 * plural case that can appear in the user's language and the
39 * <code>PluralFormat</code> selects the appropriate message based on
42 * <h4>The Problem of Plural Forms in Internationalized Messages</h4>
44 * Different languages have different ways to inflect
45 * plurals. Creating internationalized messages that include plural
46 * forms is only feasible when the framework is able to handle plural
47 * forms of <i>all</i> languages correctly. <code>ChoiceFormat</code>
48 * doesn't handle this well, because it attaches a number interval to
49 * each message and selects the message whose interval contains a
50 * given number. This can only handle a finite number of
51 * intervals. But in some languages, like Polish, one plural case
52 * applies to infinitely many intervals (e.g., the plural case applies to
53 * numbers ending with 2, 3, or 4 except those ending with 12, 13, or
54 * 14). Thus <code>ChoiceFormat</code> is not adequate.
56 * <code>PluralFormat</code> deals with this by breaking the problem
59 * <li>It uses <code>PluralRules</code> that can define more complex
60 * conditions for a plural case than just a single interval. These plural
61 * rules define both what plural cases exist in a language, and to
62 * which numbers these cases apply.
63 * <li>It provides predefined plural rules for many languages. Thus, the programmer
64 * need not worry about the plural cases of a language and
65 * does not have to define the plural cases; they can simply
66 * use the predefined keywords. The whole plural formatting of messages can
67 * be done using localized patterns from resource bundles. For predefined plural
68 * rules, see the CLDR <i>Language Plural Rules</i> page at
69 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
72 * <h4>Usage of <code>PluralFormat</code></h4>
73 * <p>Note: Typically, plural formatting is done via <code>MessageFormat</code>
74 * with a <code>plural</code> argument type,
75 * rather than using a stand-alone <code>PluralFormat</code>.
77 * This discussion assumes that you use <code>PluralFormat</code> with
78 * a predefined set of plural rules. You can create one using one of
79 * the constructors that takes a <code>locale</code> object. To
80 * specify the message pattern, you can either pass it to the
81 * constructor or set it explicitly using the
82 * <code>applyPattern()</code> method. The <code>format()</code>
83 * method takes a number object and selects the message of the
84 * matching plural case. This message will be returned.
86 * <h5>Patterns and Their Interpretation</h5>
88 * The pattern text defines the message output for each plural case of the
89 * specified locale. Syntax:
91 * pluralStyle = [offsetValue] (selector '{' message '}')+
92 * offsetValue = "offset:" number
93 * selector = explicitValue | keyword
94 * explicitValue = '=' number // adjacent, no white space in between
95 * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
96 * message: see {@link MessageFormat}
98 * Pattern_White_Space between syntax elements is ignored, except
99 * between the {curly braces} and their sub-message,
100 * and between the '=' and the number of an explicitValue.
103 * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and
104 * 'other'. You always have to define a message text for the default plural case
105 * <code>other</code> which is contained in every rule set.
106 * If you do not specify a message text for a particular plural case, the
107 * message text of the plural case <code>other</code> gets assigned to this
110 * When formatting, the input number is first matched against the explicitValue clauses.
111 * If there is no exact-number match, then a keyword is selected by calling
112 * the <code>PluralRules</code> with the input number <em>minus the offset</em>.
113 * (The offset defaults to 0 if it is omitted from the pattern string.)
114 * If there is no clause with that keyword, then the "other" clauses is returned.
116 * An unquoted pound sign (<code>#</code>) in the selected sub-message
117 * itself (i.e., outside of arguments nested in the sub-message)
118 * is replaced by the input number minus the offset.
119 * The number-minus-offset value is formatted using a
120 * <code>NumberFormat</code> for the <code>PluralFormat</code>'s locale. If you
121 * need special number formatting, you have to use a <code>MessageFormat</code>
122 * and explicitly specify a <code>NumberFormat</code> argument.
123 * <strong>Note:</strong> That argument is formatting without subtracting the offset!
124 * If you need a custom format and have a non-zero offset, then you need to pass the
125 * number-minus-offset value as a separate parameter.
127 * For a usage example, see the {@link MessageFormat} class documentation.
129 * <h4>Defining Custom Plural Rules</h4>
130 * <p>If you need to use <code>PluralFormat</code> with custom rules, you can
131 * create a <code>PluralRules</code> object and pass it to
132 * <code>PluralFormat</code>'s constructor. If you also specify a locale in this
133 * constructor, this locale will be used to format the number in the message
136 * For more information about <code>PluralRules</code>, see
137 * {@link PluralRules}.
144 class U_I18N_API PluralFormat
: public Format
{
148 * Creates a new cardinal-number <code>PluralFormat</code> for the default locale.
149 * This locale will be used to get the set of plural rules and for standard
151 * @param status output param set to success/failure code on exit, which
152 * must not indicate a failure before the function call.
155 PluralFormat(UErrorCode
& status
);
158 * Creates a new cardinal-number <code>PluralFormat</code> for a given locale.
159 * @param locale the <code>PluralFormat</code> will be configured with
160 * rules for this locale. This locale will also be used for
161 * standard number formatting.
162 * @param status output param set to success/failure code on exit, which
163 * must not indicate a failure before the function call.
166 PluralFormat(const Locale
& locale
, UErrorCode
& status
);
169 * Creates a new <code>PluralFormat</code> for a given set of rules.
170 * The standard number formatting will be done using the default locale.
171 * @param rules defines the behavior of the <code>PluralFormat</code>
173 * @param status output param set to success/failure code on exit, which
174 * must not indicate a failure before the function call.
177 PluralFormat(const PluralRules
& rules
, UErrorCode
& status
);
180 * Creates a new <code>PluralFormat</code> for a given set of rules.
181 * The standard number formatting will be done using the given locale.
182 * @param locale the default number formatting will be done using this
184 * @param rules defines the behavior of the <code>PluralFormat</code>
186 * @param status output param set to success/failure code on exit, which
187 * must not indicate a failure before the function call.
190 * <h4>Sample code</h4>
191 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample1
192 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample
195 PluralFormat(const Locale
& locale
, const PluralRules
& rules
, UErrorCode
& status
);
198 * Creates a new <code>PluralFormat</code> for the plural type.
199 * The standard number formatting will be done using the given locale.
200 * @param locale the default number formatting will be done using this
202 * @param type The plural type (e.g., cardinal or ordinal).
203 * @param status output param set to success/failure code on exit, which
204 * must not indicate a failure before the function call.
207 PluralFormat(const Locale
& locale
, UPluralType type
, UErrorCode
& status
);
210 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string.
211 * The default locale will be used to get the set of plural rules and for
212 * standard number formatting.
213 * @param pattern the pattern for this <code>PluralFormat</code>.
214 * errors are returned to status if the pattern is invalid.
215 * @param status output param set to success/failure code on exit, which
216 * must not indicate a failure before the function call.
219 PluralFormat(const UnicodeString
& pattern
, UErrorCode
& status
);
222 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string and
224 * The locale will be used to get the set of plural rules and for
225 * standard number formatting.
226 * @param locale the <code>PluralFormat</code> will be configured with
227 * rules for this locale. This locale will also be used for
228 * standard number formatting.
229 * @param pattern the pattern for this <code>PluralFormat</code>.
230 * errors are returned to status if the pattern is invalid.
231 * @param status output param set to success/failure code on exit, which
232 * must not indicate a failure before the function call.
235 PluralFormat(const Locale
& locale
, const UnicodeString
& pattern
, UErrorCode
& status
);
238 * Creates a new <code>PluralFormat</code> for a given set of rules, a
239 * pattern and a locale.
240 * @param rules defines the behavior of the <code>PluralFormat</code>
242 * @param pattern the pattern for this <code>PluralFormat</code>.
243 * errors are returned to status if the pattern is invalid.
244 * @param status output param set to success/failure code on exit, which
245 * must not indicate a failure before the function call.
248 PluralFormat(const PluralRules
& rules
,
249 const UnicodeString
& pattern
,
253 * Creates a new <code>PluralFormat</code> for a given set of rules, a
254 * pattern and a locale.
255 * @param locale the <code>PluralFormat</code> will be configured with
256 * rules for this locale. This locale will also be used for
257 * standard number formatting.
258 * @param rules defines the behavior of the <code>PluralFormat</code>
260 * @param pattern the pattern for this <code>PluralFormat</code>.
261 * errors are returned to status if the pattern is invalid.
262 * @param status output param set to success/failure code on exit, which
263 * must not indicate a failure before the function call.
266 PluralFormat(const Locale
& locale
,
267 const PluralRules
& rules
,
268 const UnicodeString
& pattern
,
272 * Creates a new <code>PluralFormat</code> for a plural type, a
273 * pattern and a locale.
274 * @param locale the <code>PluralFormat</code> will be configured with
275 * rules for this locale. This locale will also be used for
276 * standard number formatting.
277 * @param type The plural type (e.g., cardinal or ordinal).
278 * @param pattern the pattern for this <code>PluralFormat</code>.
279 * errors are returned to status if the pattern is invalid.
280 * @param status output param set to success/failure code on exit, which
281 * must not indicate a failure before the function call.
284 PluralFormat(const Locale
& locale
,
286 const UnicodeString
& pattern
,
293 PluralFormat(const PluralFormat
& other
);
299 virtual ~PluralFormat();
302 * Sets the pattern used by this plural format.
303 * The method parses the pattern and creates a map of format strings
304 * for the plural rules.
305 * Patterns and their interpretation are specified in the class description.
307 * @param pattern the pattern for this plural format
308 * errors are returned to status if the pattern is invalid.
309 * @param status output param set to success/failure code on exit, which
310 * must not indicate a failure before the function call.
313 void applyPattern(const UnicodeString
& pattern
, UErrorCode
& status
);
316 using Format::format
;
319 * Formats a plural message for a given number.
321 * @param number a number for which the plural message should be formatted
322 * for. If no pattern has been applied to this
323 * <code>PluralFormat</code> object yet, the formatted number
325 * @param status output param set to success/failure code on exit, which
326 * must not indicate a failure before the function call.
327 * @return the string containing the formatted plural message.
330 UnicodeString
format(int32_t number
, UErrorCode
& status
) const;
333 * Formats a plural message for a given number.
335 * @param number a number for which the plural message should be formatted
336 * for. If no pattern has been applied to this
337 * PluralFormat object yet, the formatted number
339 * @param status output param set to success or failure code on exit, which
340 * must not indicate a failure before the function call.
341 * @return the string containing the formatted plural message.
344 UnicodeString
format(double number
, UErrorCode
& status
) const;
347 * Formats a plural message for a given number.
349 * @param number a number for which the plural message should be formatted
350 * for. If no pattern has been applied to this
351 * <code>PluralFormat</code> object yet, the formatted number
353 * @param appendTo output parameter to receive result.
354 * result is appended to existing contents.
355 * @param pos On input: an alignment field, if desired.
356 * On output: the offsets of the alignment field.
357 * @param status output param set to success/failure code on exit, which
358 * must not indicate a failure before the function call.
359 * @return the string containing the formatted plural message.
362 UnicodeString
& format(int32_t number
,
363 UnicodeString
& appendTo
,
365 UErrorCode
& status
) const;
368 * Formats a plural message for a given number.
370 * @param number a number for which the plural message should be formatted
371 * for. If no pattern has been applied to this
372 * PluralFormat object yet, the formatted number
374 * @param appendTo output parameter to receive result.
375 * result is appended to existing contents.
376 * @param pos On input: an alignment field, if desired.
377 * On output: the offsets of the alignment field.
378 * @param status output param set to success/failure code on exit, which
379 * must not indicate a failure before the function call.
380 * @return the string containing the formatted plural message.
383 UnicodeString
& format(double number
,
384 UnicodeString
& appendTo
,
386 UErrorCode
& status
) const;
388 #ifndef U_HIDE_DEPRECATED_API
390 * Sets the locale used by this <code>PluraFormat</code> object.
391 * Note: Calling this method resets this <code>PluraFormat</code> object,
392 * i.e., a pattern that was applied previously will be removed,
393 * and the NumberFormat is set to the default number format for
394 * the locale. The resulting format behaves the same as one
395 * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)}
396 * with UPLURAL_TYPE_CARDINAL.
397 * @param locale the <code>locale</code> to use to configure the formatter.
398 * @param status output param set to success/failure code on exit, which
399 * must not indicate a failure before the function call.
400 * @deprecated ICU 50 This method clears the pattern and might create
401 * a different kind of PluralRules instance;
402 * use one of the constructors to create a new instance instead.
404 void setLocale(const Locale
& locale
, UErrorCode
& status
);
405 #endif /* U_HIDE_DEPRECATED_API */
408 * Sets the number format used by this formatter. You only need to
409 * call this if you want a different number format than the default
410 * formatter for the locale.
411 * @param format the number format to use.
412 * @param status output param set to success/failure code on exit, which
413 * must not indicate a failure before the function call.
416 void setNumberFormat(const NumberFormat
* format
, UErrorCode
& status
);
419 * Assignment operator
421 * @param other the PluralFormat object to copy from.
424 PluralFormat
& operator=(const PluralFormat
& other
);
427 * Return true if another object is semantically equal to this one.
429 * @param other the PluralFormat object to be compared with.
430 * @return true if other is semantically equal to this.
433 virtual UBool
operator==(const Format
& other
) const;
436 * Return true if another object is semantically unequal to this one.
438 * @param other the PluralFormat object to be compared with.
439 * @return true if other is semantically unequal to this.
442 virtual UBool
operator!=(const Format
& other
) const;
445 * Clones this Format object polymorphically. The caller owns the
446 * result and should delete it when done.
449 virtual Format
* clone(void) const;
452 * Formats a plural message for a number taken from a Formattable object.
454 * @param obj The object containing a number for which the
455 * plural message should be formatted.
456 * The object must be of a numeric type.
457 * @param appendTo output parameter to receive result.
458 * Result is appended to existing contents.
459 * @param pos On input: an alignment field, if desired.
460 * On output: the offsets of the alignment field.
461 * @param status output param filled with success/failure status.
462 * @return Reference to 'appendTo' parameter.
465 UnicodeString
& format(const Formattable
& obj
,
466 UnicodeString
& appendTo
,
468 UErrorCode
& status
) const;
471 * Returns the pattern from applyPattern() or constructor().
473 * @param appendTo output parameter to receive result.
474 * Result is appended to existing contents.
475 * @return the UnicodeString with inserted pattern.
478 UnicodeString
& toPattern(UnicodeString
& appendTo
);
481 * This method is not yet supported by <code>PluralFormat</code>.
483 * Before calling, set parse_pos.index to the offset you want to start
484 * parsing at in the source. After calling, parse_pos.index is the end of
485 * the text you parsed. If error occurs, index is unchanged.
487 * When parsing, leading whitespace is discarded (with a successful parse),
488 * while trailing whitespace is left as is.
490 * See Format::parseObject() for more.
492 * @param source The string to be parsed into an object.
493 * @param result Formattable to be set to the parse result.
494 * If parse fails, return contents are undefined.
495 * @param parse_pos The position to start parsing at. Upon return
496 * this param is set to the position after the
497 * last character successfully parsed. If the
498 * source is not parsed successfully, this param
499 * will remain unchanged.
502 virtual void parseObject(const UnicodeString
& source
,
504 ParsePosition
& parse_pos
) const;
507 * ICU "poor man's RTTI", returns a UClassID for this class.
512 static UClassID U_EXPORT2
getStaticClassID(void);
515 * ICU "poor man's RTTI", returns a UClassID for the actual class.
519 virtual UClassID
getDynamicClassID() const;
521 #if (defined(__xlC__) && (__xlC__ < 0x0C00)) || (U_PLATFORM == U_PF_OS390) || (U_PLATFORM ==U_PF_OS400)
522 // Work around a compiler bug on xlC 11.1 on AIX 7.1 that would
523 // prevent PluralSelectorAdapter from implementing private PluralSelector.
524 // xlC error message:
525 // 1540-0300 (S) The "private" member "class icu_49::PluralFormat::PluralSelector" cannot be accessed.
533 class U_I18N_API PluralSelector
: public UMemory
{
535 virtual ~PluralSelector();
537 * Given a number, returns the appropriate PluralFormat keyword.
539 * @param context worker object for the selector.
540 * @param number The number to be plural-formatted.
541 * @param ec Error code.
542 * @return The selected PluralFormat keyword.
545 virtual UnicodeString
select(void *context
, double number
, UErrorCode
& ec
) const = 0;
551 class U_I18N_API PluralSelectorAdapter
: public PluralSelector
{
553 PluralSelectorAdapter() : pluralRules(NULL
) {
556 virtual ~PluralSelectorAdapter();
558 virtual UnicodeString
select(void *context
, double number
, UErrorCode
& /*ec*/) const; /**< @internal */
562 PluralRules
* pluralRules
;
566 // End of xlC bug workaround, keep remaining definitions private.
570 MessagePattern msgPattern
;
571 NumberFormat
* numberFormat
;
573 PluralSelectorAdapter pluralRulesWrapper
;
575 PluralFormat(); // default constructor not implemented
576 void init(const PluralRules
* rules
, UPluralType type
, UErrorCode
& status
);
578 * Copies dynamically allocated values (pointer fields).
579 * Others are copied using their copy constructors and assignment operators.
581 void copyObjects(const PluralFormat
& other
);
583 UnicodeString
& format(const Formattable
& numberObject
, double number
,
584 UnicodeString
& appendTo
,
586 UErrorCode
& status
) const; /**< @internal */
589 * Finds the PluralFormat sub-message for the given number, or the "other" sub-message.
590 * @param pattern A MessagePattern.
591 * @param partIndex the index of the first PluralFormat argument style part.
592 * @param selector the PluralSelector for mapping the number (minus offset) to a keyword.
593 * @param context worker object for the selector.
594 * @param number a number to be matched to one of the PluralFormat argument's explicit values,
595 * or mapped via the PluralSelector.
596 * @param ec ICU error code.
597 * @return the sub-message start part index.
599 static int32_t findSubMessage(
600 const MessagePattern
& pattern
, int32_t partIndex
,
601 const PluralSelector
& selector
, void *context
, double number
, UErrorCode
& ec
); /**< @internal */
603 void parseType(const UnicodeString
& source
, const NFRule
*rbnfLenientScanner
,
604 Formattable
& result
, FieldPosition
& pos
) const;
606 friend class MessageFormat
;
612 #endif /* #if !UCONFIG_NO_FORMATTING */