12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others. 22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html 32e5b6d6dSopenharmony_ci/* 42e5b6d6dSopenharmony_ci******************************************************************************* 52e5b6d6dSopenharmony_ci* Copyright (C) 2007-2014, International Business Machines Corporation and 62e5b6d6dSopenharmony_ci* others. All Rights Reserved. 72e5b6d6dSopenharmony_ci******************************************************************************* 82e5b6d6dSopenharmony_ci* 92e5b6d6dSopenharmony_ci 102e5b6d6dSopenharmony_ci* File PLURFMT.H 112e5b6d6dSopenharmony_ci******************************************************************************** 122e5b6d6dSopenharmony_ci*/ 132e5b6d6dSopenharmony_ci 142e5b6d6dSopenharmony_ci#ifndef PLURFMT 152e5b6d6dSopenharmony_ci#define PLURFMT 162e5b6d6dSopenharmony_ci 172e5b6d6dSopenharmony_ci#include "unicode/utypes.h" 182e5b6d6dSopenharmony_ci 192e5b6d6dSopenharmony_ci#if U_SHOW_CPLUSPLUS_API 202e5b6d6dSopenharmony_ci 212e5b6d6dSopenharmony_ci/** 222e5b6d6dSopenharmony_ci * \file 232e5b6d6dSopenharmony_ci * \brief C++ API: PluralFormat object 242e5b6d6dSopenharmony_ci */ 252e5b6d6dSopenharmony_ci 262e5b6d6dSopenharmony_ci#if !UCONFIG_NO_FORMATTING 272e5b6d6dSopenharmony_ci 282e5b6d6dSopenharmony_ci#include "unicode/messagepattern.h" 292e5b6d6dSopenharmony_ci#include "unicode/numfmt.h" 302e5b6d6dSopenharmony_ci#include "unicode/plurrule.h" 312e5b6d6dSopenharmony_ci 322e5b6d6dSopenharmony_ciU_NAMESPACE_BEGIN 332e5b6d6dSopenharmony_ci 342e5b6d6dSopenharmony_ciclass Hashtable; 352e5b6d6dSopenharmony_ciclass NFRule; 362e5b6d6dSopenharmony_ci 372e5b6d6dSopenharmony_ci/** 382e5b6d6dSopenharmony_ci * <p> 392e5b6d6dSopenharmony_ci * <code>PluralFormat</code> supports the creation of internationalized 402e5b6d6dSopenharmony_ci * messages with plural inflection. It is based on <i>plural 412e5b6d6dSopenharmony_ci * selection</i>, i.e. the caller specifies messages for each 422e5b6d6dSopenharmony_ci * plural case that can appear in the user's language and the 432e5b6d6dSopenharmony_ci * <code>PluralFormat</code> selects the appropriate message based on 442e5b6d6dSopenharmony_ci * the number. 452e5b6d6dSopenharmony_ci * </p> 462e5b6d6dSopenharmony_ci * <h4>The Problem of Plural Forms in Internationalized Messages</h4> 472e5b6d6dSopenharmony_ci * <p> 482e5b6d6dSopenharmony_ci * Different languages have different ways to inflect 492e5b6d6dSopenharmony_ci * plurals. Creating internationalized messages that include plural 502e5b6d6dSopenharmony_ci * forms is only feasible when the framework is able to handle plural 512e5b6d6dSopenharmony_ci * forms of <i>all</i> languages correctly. <code>ChoiceFormat</code> 522e5b6d6dSopenharmony_ci * doesn't handle this well, because it attaches a number interval to 532e5b6d6dSopenharmony_ci * each message and selects the message whose interval contains a 542e5b6d6dSopenharmony_ci * given number. This can only handle a finite number of 552e5b6d6dSopenharmony_ci * intervals. But in some languages, like Polish, one plural case 562e5b6d6dSopenharmony_ci * applies to infinitely many intervals (e.g., the plural case applies to 572e5b6d6dSopenharmony_ci * numbers ending with 2, 3, or 4 except those ending with 12, 13, or 582e5b6d6dSopenharmony_ci * 14). Thus <code>ChoiceFormat</code> is not adequate. 592e5b6d6dSopenharmony_ci * </p><p> 602e5b6d6dSopenharmony_ci * <code>PluralFormat</code> deals with this by breaking the problem 612e5b6d6dSopenharmony_ci * into two parts: 622e5b6d6dSopenharmony_ci * <ul> 632e5b6d6dSopenharmony_ci * <li>It uses <code>PluralRules</code> that can define more complex 642e5b6d6dSopenharmony_ci * conditions for a plural case than just a single interval. These plural 652e5b6d6dSopenharmony_ci * rules define both what plural cases exist in a language, and to 662e5b6d6dSopenharmony_ci * which numbers these cases apply. 672e5b6d6dSopenharmony_ci * <li>It provides predefined plural rules for many languages. Thus, the programmer 682e5b6d6dSopenharmony_ci * need not worry about the plural cases of a language and 692e5b6d6dSopenharmony_ci * does not have to define the plural cases; they can simply 702e5b6d6dSopenharmony_ci * use the predefined keywords. The whole plural formatting of messages can 712e5b6d6dSopenharmony_ci * be done using localized patterns from resource bundles. For predefined plural 722e5b6d6dSopenharmony_ci * rules, see the CLDR <i>Language Plural Rules</i> page at 732e5b6d6dSopenharmony_ci * https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html 742e5b6d6dSopenharmony_ci * </ul> 752e5b6d6dSopenharmony_ci * </p> 762e5b6d6dSopenharmony_ci * <h4>Usage of <code>PluralFormat</code></h4> 772e5b6d6dSopenharmony_ci * <p>Note: Typically, plural formatting is done via <code>MessageFormat</code> 782e5b6d6dSopenharmony_ci * with a <code>plural</code> argument type, 792e5b6d6dSopenharmony_ci * rather than using a stand-alone <code>PluralFormat</code>. 802e5b6d6dSopenharmony_ci * </p><p> 812e5b6d6dSopenharmony_ci * This discussion assumes that you use <code>PluralFormat</code> with 822e5b6d6dSopenharmony_ci * a predefined set of plural rules. You can create one using one of 832e5b6d6dSopenharmony_ci * the constructors that takes a <code>locale</code> object. To 842e5b6d6dSopenharmony_ci * specify the message pattern, you can either pass it to the 852e5b6d6dSopenharmony_ci * constructor or set it explicitly using the 862e5b6d6dSopenharmony_ci * <code>applyPattern()</code> method. The <code>format()</code> 872e5b6d6dSopenharmony_ci * method takes a number object and selects the message of the 882e5b6d6dSopenharmony_ci * matching plural case. This message will be returned. 892e5b6d6dSopenharmony_ci * </p> 902e5b6d6dSopenharmony_ci * <h5>Patterns and Their Interpretation</h5> 912e5b6d6dSopenharmony_ci * <p> 922e5b6d6dSopenharmony_ci * The pattern text defines the message output for each plural case of the 932e5b6d6dSopenharmony_ci * specified locale. Syntax: 942e5b6d6dSopenharmony_ci * <pre> 952e5b6d6dSopenharmony_ci * pluralStyle = [offsetValue] (selector '{' message '}')+ 962e5b6d6dSopenharmony_ci * offsetValue = "offset:" number 972e5b6d6dSopenharmony_ci * selector = explicitValue | keyword 982e5b6d6dSopenharmony_ci * explicitValue = '=' number // adjacent, no white space in between 992e5b6d6dSopenharmony_ci * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ 1002e5b6d6dSopenharmony_ci * message: see {@link MessageFormat} 1012e5b6d6dSopenharmony_ci * </pre> 1022e5b6d6dSopenharmony_ci * Pattern_White_Space between syntax elements is ignored, except 1032e5b6d6dSopenharmony_ci * between the {curly braces} and their sub-message, 1042e5b6d6dSopenharmony_ci * and between the '=' and the number of an explicitValue. 1052e5b6d6dSopenharmony_ci * 1062e5b6d6dSopenharmony_ci * </p><p> 1072e5b6d6dSopenharmony_ci * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and 1082e5b6d6dSopenharmony_ci * 'other'. You always have to define a message text for the default plural case 1092e5b6d6dSopenharmony_ci * <code>other</code> which is contained in every rule set. 1102e5b6d6dSopenharmony_ci * If you do not specify a message text for a particular plural case, the 1112e5b6d6dSopenharmony_ci * message text of the plural case <code>other</code> gets assigned to this 1122e5b6d6dSopenharmony_ci * plural case. 1132e5b6d6dSopenharmony_ci * </p><p> 1142e5b6d6dSopenharmony_ci * When formatting, the input number is first matched against the explicitValue clauses. 1152e5b6d6dSopenharmony_ci * If there is no exact-number match, then a keyword is selected by calling 1162e5b6d6dSopenharmony_ci * the <code>PluralRules</code> with the input number <em>minus the offset</em>. 1172e5b6d6dSopenharmony_ci * (The offset defaults to 0 if it is omitted from the pattern string.) 1182e5b6d6dSopenharmony_ci * If there is no clause with that keyword, then the "other" clauses is returned. 1192e5b6d6dSopenharmony_ci * </p><p> 1202e5b6d6dSopenharmony_ci * An unquoted pound sign (<code>#</code>) in the selected sub-message 1212e5b6d6dSopenharmony_ci * itself (i.e., outside of arguments nested in the sub-message) 1222e5b6d6dSopenharmony_ci * is replaced by the input number minus the offset. 1232e5b6d6dSopenharmony_ci * The number-minus-offset value is formatted using a 1242e5b6d6dSopenharmony_ci * <code>NumberFormat</code> for the <code>PluralFormat</code>'s locale. If you 1252e5b6d6dSopenharmony_ci * need special number formatting, you have to use a <code>MessageFormat</code> 1262e5b6d6dSopenharmony_ci * and explicitly specify a <code>NumberFormat</code> argument. 1272e5b6d6dSopenharmony_ci * <strong>Note:</strong> That argument is formatting without subtracting the offset! 1282e5b6d6dSopenharmony_ci * If you need a custom format and have a non-zero offset, then you need to pass the 1292e5b6d6dSopenharmony_ci * number-minus-offset value as a separate parameter. 1302e5b6d6dSopenharmony_ci * </p> 1312e5b6d6dSopenharmony_ci * For a usage example, see the {@link MessageFormat} class documentation. 1322e5b6d6dSopenharmony_ci * 1332e5b6d6dSopenharmony_ci * <h4>Defining Custom Plural Rules</h4> 1342e5b6d6dSopenharmony_ci * <p>If you need to use <code>PluralFormat</code> with custom rules, you can 1352e5b6d6dSopenharmony_ci * create a <code>PluralRules</code> object and pass it to 1362e5b6d6dSopenharmony_ci * <code>PluralFormat</code>'s constructor. If you also specify a locale in this 1372e5b6d6dSopenharmony_ci * constructor, this locale will be used to format the number in the message 1382e5b6d6dSopenharmony_ci * texts. 1392e5b6d6dSopenharmony_ci * </p><p> 1402e5b6d6dSopenharmony_ci * For more information about <code>PluralRules</code>, see 1412e5b6d6dSopenharmony_ci * {@link PluralRules}. 1422e5b6d6dSopenharmony_ci * </p> 1432e5b6d6dSopenharmony_ci * 1442e5b6d6dSopenharmony_ci * ported from Java 1452e5b6d6dSopenharmony_ci * @stable ICU 4.0 1462e5b6d6dSopenharmony_ci */ 1472e5b6d6dSopenharmony_ci 1482e5b6d6dSopenharmony_ciclass U_I18N_API PluralFormat : public Format { 1492e5b6d6dSopenharmony_cipublic: 1502e5b6d6dSopenharmony_ci 1512e5b6d6dSopenharmony_ci /** 1522e5b6d6dSopenharmony_ci * Creates a new cardinal-number <code>PluralFormat</code> for the default locale. 1532e5b6d6dSopenharmony_ci * This locale will be used to get the set of plural rules and for standard 1542e5b6d6dSopenharmony_ci * number formatting. 1552e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 1562e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 1572e5b6d6dSopenharmony_ci * @stable ICU 4.0 1582e5b6d6dSopenharmony_ci */ 1592e5b6d6dSopenharmony_ci PluralFormat(UErrorCode& status); 1602e5b6d6dSopenharmony_ci 1612e5b6d6dSopenharmony_ci /** 1622e5b6d6dSopenharmony_ci * Creates a new cardinal-number <code>PluralFormat</code> for a given locale. 1632e5b6d6dSopenharmony_ci * @param locale the <code>PluralFormat</code> will be configured with 1642e5b6d6dSopenharmony_ci * rules for this locale. This locale will also be used for 1652e5b6d6dSopenharmony_ci * standard number formatting. 1662e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 1672e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 1682e5b6d6dSopenharmony_ci * @stable ICU 4.0 1692e5b6d6dSopenharmony_ci */ 1702e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, UErrorCode& status); 1712e5b6d6dSopenharmony_ci 1722e5b6d6dSopenharmony_ci /** 1732e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for a given set of rules. 1742e5b6d6dSopenharmony_ci * The standard number formatting will be done using the default locale. 1752e5b6d6dSopenharmony_ci * @param rules defines the behavior of the <code>PluralFormat</code> 1762e5b6d6dSopenharmony_ci * object. 1772e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 1782e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 1792e5b6d6dSopenharmony_ci * @stable ICU 4.0 1802e5b6d6dSopenharmony_ci */ 1812e5b6d6dSopenharmony_ci PluralFormat(const PluralRules& rules, UErrorCode& status); 1822e5b6d6dSopenharmony_ci 1832e5b6d6dSopenharmony_ci /** 1842e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for a given set of rules. 1852e5b6d6dSopenharmony_ci * The standard number formatting will be done using the given locale. 1862e5b6d6dSopenharmony_ci * @param locale the default number formatting will be done using this 1872e5b6d6dSopenharmony_ci * locale. 1882e5b6d6dSopenharmony_ci * @param rules defines the behavior of the <code>PluralFormat</code> 1892e5b6d6dSopenharmony_ci * object. 1902e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 1912e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 1922e5b6d6dSopenharmony_ci * @stable ICU 4.0 1932e5b6d6dSopenharmony_ci * <p> 1942e5b6d6dSopenharmony_ci * <h4>Sample code</h4> 1952e5b6d6dSopenharmony_ci * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample1 1962e5b6d6dSopenharmony_ci * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample 1972e5b6d6dSopenharmony_ci * <p> 1982e5b6d6dSopenharmony_ci */ 1992e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status); 2002e5b6d6dSopenharmony_ci 2012e5b6d6dSopenharmony_ci /** 2022e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for the plural type. 2032e5b6d6dSopenharmony_ci * The standard number formatting will be done using the given locale. 2042e5b6d6dSopenharmony_ci * @param locale the default number formatting will be done using this 2052e5b6d6dSopenharmony_ci * locale. 2062e5b6d6dSopenharmony_ci * @param type The plural type (e.g., cardinal or ordinal). 2072e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2082e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2092e5b6d6dSopenharmony_ci * @stable ICU 50 2102e5b6d6dSopenharmony_ci */ 2112e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status); 2122e5b6d6dSopenharmony_ci 2132e5b6d6dSopenharmony_ci /** 2142e5b6d6dSopenharmony_ci * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string. 2152e5b6d6dSopenharmony_ci * The default locale will be used to get the set of plural rules and for 2162e5b6d6dSopenharmony_ci * standard number formatting. 2172e5b6d6dSopenharmony_ci * @param pattern the pattern for this <code>PluralFormat</code>. 2182e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 2192e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2202e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2212e5b6d6dSopenharmony_ci * @stable ICU 4.0 2222e5b6d6dSopenharmony_ci */ 2232e5b6d6dSopenharmony_ci PluralFormat(const UnicodeString& pattern, UErrorCode& status); 2242e5b6d6dSopenharmony_ci 2252e5b6d6dSopenharmony_ci /** 2262e5b6d6dSopenharmony_ci * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string and 2272e5b6d6dSopenharmony_ci * locale. 2282e5b6d6dSopenharmony_ci * The locale will be used to get the set of plural rules and for 2292e5b6d6dSopenharmony_ci * standard number formatting. 2302e5b6d6dSopenharmony_ci * @param locale the <code>PluralFormat</code> will be configured with 2312e5b6d6dSopenharmony_ci * rules for this locale. This locale will also be used for 2322e5b6d6dSopenharmony_ci * standard number formatting. 2332e5b6d6dSopenharmony_ci * @param pattern the pattern for this <code>PluralFormat</code>. 2342e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 2352e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2362e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2372e5b6d6dSopenharmony_ci * @stable ICU 4.0 2382e5b6d6dSopenharmony_ci */ 2392e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status); 2402e5b6d6dSopenharmony_ci 2412e5b6d6dSopenharmony_ci /** 2422e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for a given set of rules, a 2432e5b6d6dSopenharmony_ci * pattern and a locale. 2442e5b6d6dSopenharmony_ci * @param rules defines the behavior of the <code>PluralFormat</code> 2452e5b6d6dSopenharmony_ci * object. 2462e5b6d6dSopenharmony_ci * @param pattern the pattern for this <code>PluralFormat</code>. 2472e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 2482e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2492e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2502e5b6d6dSopenharmony_ci * @stable ICU 4.0 2512e5b6d6dSopenharmony_ci */ 2522e5b6d6dSopenharmony_ci PluralFormat(const PluralRules& rules, 2532e5b6d6dSopenharmony_ci const UnicodeString& pattern, 2542e5b6d6dSopenharmony_ci UErrorCode& status); 2552e5b6d6dSopenharmony_ci 2562e5b6d6dSopenharmony_ci /** 2572e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for a given set of rules, a 2582e5b6d6dSopenharmony_ci * pattern and a locale. 2592e5b6d6dSopenharmony_ci * @param locale the <code>PluralFormat</code> will be configured with 2602e5b6d6dSopenharmony_ci * rules for this locale. This locale will also be used for 2612e5b6d6dSopenharmony_ci * standard number formatting. 2622e5b6d6dSopenharmony_ci * @param rules defines the behavior of the <code>PluralFormat</code> 2632e5b6d6dSopenharmony_ci * object. 2642e5b6d6dSopenharmony_ci * @param pattern the pattern for this <code>PluralFormat</code>. 2652e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 2662e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2672e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2682e5b6d6dSopenharmony_ci * @stable ICU 4.0 2692e5b6d6dSopenharmony_ci */ 2702e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, 2712e5b6d6dSopenharmony_ci const PluralRules& rules, 2722e5b6d6dSopenharmony_ci const UnicodeString& pattern, 2732e5b6d6dSopenharmony_ci UErrorCode& status); 2742e5b6d6dSopenharmony_ci 2752e5b6d6dSopenharmony_ci /** 2762e5b6d6dSopenharmony_ci * Creates a new <code>PluralFormat</code> for a plural type, a 2772e5b6d6dSopenharmony_ci * pattern and a locale. 2782e5b6d6dSopenharmony_ci * @param locale the <code>PluralFormat</code> will be configured with 2792e5b6d6dSopenharmony_ci * rules for this locale. This locale will also be used for 2802e5b6d6dSopenharmony_ci * standard number formatting. 2812e5b6d6dSopenharmony_ci * @param type The plural type (e.g., cardinal or ordinal). 2822e5b6d6dSopenharmony_ci * @param pattern the pattern for this <code>PluralFormat</code>. 2832e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 2842e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 2852e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 2862e5b6d6dSopenharmony_ci * @stable ICU 50 2872e5b6d6dSopenharmony_ci */ 2882e5b6d6dSopenharmony_ci PluralFormat(const Locale& locale, 2892e5b6d6dSopenharmony_ci UPluralType type, 2902e5b6d6dSopenharmony_ci const UnicodeString& pattern, 2912e5b6d6dSopenharmony_ci UErrorCode& status); 2922e5b6d6dSopenharmony_ci 2932e5b6d6dSopenharmony_ci /** 2942e5b6d6dSopenharmony_ci * copy constructor. 2952e5b6d6dSopenharmony_ci * @stable ICU 4.0 2962e5b6d6dSopenharmony_ci */ 2972e5b6d6dSopenharmony_ci PluralFormat(const PluralFormat& other); 2982e5b6d6dSopenharmony_ci 2992e5b6d6dSopenharmony_ci /** 3002e5b6d6dSopenharmony_ci * Destructor. 3012e5b6d6dSopenharmony_ci * @stable ICU 4.0 3022e5b6d6dSopenharmony_ci */ 3032e5b6d6dSopenharmony_ci virtual ~PluralFormat(); 3042e5b6d6dSopenharmony_ci 3052e5b6d6dSopenharmony_ci /** 3062e5b6d6dSopenharmony_ci * Sets the pattern used by this plural format. 3072e5b6d6dSopenharmony_ci * The method parses the pattern and creates a map of format strings 3082e5b6d6dSopenharmony_ci * for the plural rules. 3092e5b6d6dSopenharmony_ci * Patterns and their interpretation are specified in the class description. 3102e5b6d6dSopenharmony_ci * 3112e5b6d6dSopenharmony_ci * @param pattern the pattern for this plural format 3122e5b6d6dSopenharmony_ci * errors are returned to status if the pattern is invalid. 3132e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 3142e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 3152e5b6d6dSopenharmony_ci * @stable ICU 4.0 3162e5b6d6dSopenharmony_ci */ 3172e5b6d6dSopenharmony_ci void applyPattern(const UnicodeString& pattern, UErrorCode& status); 3182e5b6d6dSopenharmony_ci 3192e5b6d6dSopenharmony_ci 3202e5b6d6dSopenharmony_ci using Format::format; 3212e5b6d6dSopenharmony_ci 3222e5b6d6dSopenharmony_ci /** 3232e5b6d6dSopenharmony_ci * Formats a plural message for a given number. 3242e5b6d6dSopenharmony_ci * 3252e5b6d6dSopenharmony_ci * @param number a number for which the plural message should be formatted 3262e5b6d6dSopenharmony_ci * for. If no pattern has been applied to this 3272e5b6d6dSopenharmony_ci * <code>PluralFormat</code> object yet, the formatted number 3282e5b6d6dSopenharmony_ci * will be returned. 3292e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 3302e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 3312e5b6d6dSopenharmony_ci * @return the string containing the formatted plural message. 3322e5b6d6dSopenharmony_ci * @stable ICU 4.0 3332e5b6d6dSopenharmony_ci */ 3342e5b6d6dSopenharmony_ci UnicodeString format(int32_t number, UErrorCode& status) const; 3352e5b6d6dSopenharmony_ci 3362e5b6d6dSopenharmony_ci /** 3372e5b6d6dSopenharmony_ci * Formats a plural message for a given number. 3382e5b6d6dSopenharmony_ci * 3392e5b6d6dSopenharmony_ci * @param number a number for which the plural message should be formatted 3402e5b6d6dSopenharmony_ci * for. If no pattern has been applied to this 3412e5b6d6dSopenharmony_ci * PluralFormat object yet, the formatted number 3422e5b6d6dSopenharmony_ci * will be returned. 3432e5b6d6dSopenharmony_ci * @param status output param set to success or failure code on exit, which 3442e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 3452e5b6d6dSopenharmony_ci * @return the string containing the formatted plural message. 3462e5b6d6dSopenharmony_ci * @stable ICU 4.0 3472e5b6d6dSopenharmony_ci */ 3482e5b6d6dSopenharmony_ci UnicodeString format(double number, UErrorCode& status) const; 3492e5b6d6dSopenharmony_ci 3502e5b6d6dSopenharmony_ci /** 3512e5b6d6dSopenharmony_ci * Formats a plural message for a given number. 3522e5b6d6dSopenharmony_ci * 3532e5b6d6dSopenharmony_ci * @param number a number for which the plural message should be formatted 3542e5b6d6dSopenharmony_ci * for. If no pattern has been applied to this 3552e5b6d6dSopenharmony_ci * <code>PluralFormat</code> object yet, the formatted number 3562e5b6d6dSopenharmony_ci * will be returned. 3572e5b6d6dSopenharmony_ci * @param appendTo output parameter to receive result. 3582e5b6d6dSopenharmony_ci * result is appended to existing contents. 3592e5b6d6dSopenharmony_ci * @param pos On input: an alignment field, if desired. 3602e5b6d6dSopenharmony_ci * On output: the offsets of the alignment field. 3612e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 3622e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 3632e5b6d6dSopenharmony_ci * @return the string containing the formatted plural message. 3642e5b6d6dSopenharmony_ci * @stable ICU 4.0 3652e5b6d6dSopenharmony_ci */ 3662e5b6d6dSopenharmony_ci UnicodeString& format(int32_t number, 3672e5b6d6dSopenharmony_ci UnicodeString& appendTo, 3682e5b6d6dSopenharmony_ci FieldPosition& pos, 3692e5b6d6dSopenharmony_ci UErrorCode& status) const; 3702e5b6d6dSopenharmony_ci 3712e5b6d6dSopenharmony_ci /** 3722e5b6d6dSopenharmony_ci * Formats a plural message for a given number. 3732e5b6d6dSopenharmony_ci * 3742e5b6d6dSopenharmony_ci * @param number a number for which the plural message should be formatted 3752e5b6d6dSopenharmony_ci * for. If no pattern has been applied to this 3762e5b6d6dSopenharmony_ci * PluralFormat object yet, the formatted number 3772e5b6d6dSopenharmony_ci * will be returned. 3782e5b6d6dSopenharmony_ci * @param appendTo output parameter to receive result. 3792e5b6d6dSopenharmony_ci * result is appended to existing contents. 3802e5b6d6dSopenharmony_ci * @param pos On input: an alignment field, if desired. 3812e5b6d6dSopenharmony_ci * On output: the offsets of the alignment field. 3822e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 3832e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 3842e5b6d6dSopenharmony_ci * @return the string containing the formatted plural message. 3852e5b6d6dSopenharmony_ci * @stable ICU 4.0 3862e5b6d6dSopenharmony_ci */ 3872e5b6d6dSopenharmony_ci UnicodeString& format(double number, 3882e5b6d6dSopenharmony_ci UnicodeString& appendTo, 3892e5b6d6dSopenharmony_ci FieldPosition& pos, 3902e5b6d6dSopenharmony_ci UErrorCode& status) const; 3912e5b6d6dSopenharmony_ci 3922e5b6d6dSopenharmony_ci#ifndef U_HIDE_DEPRECATED_API 3932e5b6d6dSopenharmony_ci /** 3942e5b6d6dSopenharmony_ci * Sets the locale used by this <code>PluraFormat</code> object. 3952e5b6d6dSopenharmony_ci * Note: Calling this method resets this <code>PluraFormat</code> object, 3962e5b6d6dSopenharmony_ci * i.e., a pattern that was applied previously will be removed, 3972e5b6d6dSopenharmony_ci * and the NumberFormat is set to the default number format for 3982e5b6d6dSopenharmony_ci * the locale. The resulting format behaves the same as one 3992e5b6d6dSopenharmony_ci * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)} 4002e5b6d6dSopenharmony_ci * with UPLURAL_TYPE_CARDINAL. 4012e5b6d6dSopenharmony_ci * @param locale the <code>locale</code> to use to configure the formatter. 4022e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 4032e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 4042e5b6d6dSopenharmony_ci * @deprecated ICU 50 This method clears the pattern and might create 4052e5b6d6dSopenharmony_ci * a different kind of PluralRules instance; 4062e5b6d6dSopenharmony_ci * use one of the constructors to create a new instance instead. 4072e5b6d6dSopenharmony_ci */ 4082e5b6d6dSopenharmony_ci void setLocale(const Locale& locale, UErrorCode& status); 4092e5b6d6dSopenharmony_ci#endif /* U_HIDE_DEPRECATED_API */ 4102e5b6d6dSopenharmony_ci 4112e5b6d6dSopenharmony_ci /** 4122e5b6d6dSopenharmony_ci * Sets the number format used by this formatter. You only need to 4132e5b6d6dSopenharmony_ci * call this if you want a different number format than the default 4142e5b6d6dSopenharmony_ci * formatter for the locale. 4152e5b6d6dSopenharmony_ci * @param format the number format to use. 4162e5b6d6dSopenharmony_ci * @param status output param set to success/failure code on exit, which 4172e5b6d6dSopenharmony_ci * must not indicate a failure before the function call. 4182e5b6d6dSopenharmony_ci * @stable ICU 4.0 4192e5b6d6dSopenharmony_ci */ 4202e5b6d6dSopenharmony_ci void setNumberFormat(const NumberFormat* format, UErrorCode& status); 4212e5b6d6dSopenharmony_ci 4222e5b6d6dSopenharmony_ci /** 4232e5b6d6dSopenharmony_ci * Assignment operator 4242e5b6d6dSopenharmony_ci * 4252e5b6d6dSopenharmony_ci * @param other the PluralFormat object to copy from. 4262e5b6d6dSopenharmony_ci * @stable ICU 4.0 4272e5b6d6dSopenharmony_ci */ 4282e5b6d6dSopenharmony_ci PluralFormat& operator=(const PluralFormat& other); 4292e5b6d6dSopenharmony_ci 4302e5b6d6dSopenharmony_ci /** 4312e5b6d6dSopenharmony_ci * Return true if another object is semantically equal to this one. 4322e5b6d6dSopenharmony_ci * 4332e5b6d6dSopenharmony_ci * @param other the PluralFormat object to be compared with. 4342e5b6d6dSopenharmony_ci * @return true if other is semantically equal to this. 4352e5b6d6dSopenharmony_ci * @stable ICU 4.0 4362e5b6d6dSopenharmony_ci */ 4372e5b6d6dSopenharmony_ci virtual bool operator==(const Format& other) const override; 4382e5b6d6dSopenharmony_ci 4392e5b6d6dSopenharmony_ci /** 4402e5b6d6dSopenharmony_ci * Return true if another object is semantically unequal to this one. 4412e5b6d6dSopenharmony_ci * 4422e5b6d6dSopenharmony_ci * @param other the PluralFormat object to be compared with. 4432e5b6d6dSopenharmony_ci * @return true if other is semantically unequal to this. 4442e5b6d6dSopenharmony_ci * @stable ICU 4.0 4452e5b6d6dSopenharmony_ci */ 4462e5b6d6dSopenharmony_ci virtual bool operator!=(const Format& other) const; 4472e5b6d6dSopenharmony_ci 4482e5b6d6dSopenharmony_ci /** 4492e5b6d6dSopenharmony_ci * Clones this Format object polymorphically. The caller owns the 4502e5b6d6dSopenharmony_ci * result and should delete it when done. 4512e5b6d6dSopenharmony_ci * @stable ICU 4.0 4522e5b6d6dSopenharmony_ci */ 4532e5b6d6dSopenharmony_ci virtual PluralFormat* clone() const override; 4542e5b6d6dSopenharmony_ci 4552e5b6d6dSopenharmony_ci /** 4562e5b6d6dSopenharmony_ci * Formats a plural message for a number taken from a Formattable object. 4572e5b6d6dSopenharmony_ci * 4582e5b6d6dSopenharmony_ci * @param obj The object containing a number for which the 4592e5b6d6dSopenharmony_ci * plural message should be formatted. 4602e5b6d6dSopenharmony_ci * The object must be of a numeric type. 4612e5b6d6dSopenharmony_ci * @param appendTo output parameter to receive result. 4622e5b6d6dSopenharmony_ci * Result is appended to existing contents. 4632e5b6d6dSopenharmony_ci * @param pos On input: an alignment field, if desired. 4642e5b6d6dSopenharmony_ci * On output: the offsets of the alignment field. 4652e5b6d6dSopenharmony_ci * @param status output param filled with success/failure status. 4662e5b6d6dSopenharmony_ci * @return Reference to 'appendTo' parameter. 4672e5b6d6dSopenharmony_ci * @stable ICU 4.0 4682e5b6d6dSopenharmony_ci */ 4692e5b6d6dSopenharmony_ci UnicodeString& format(const Formattable& obj, 4702e5b6d6dSopenharmony_ci UnicodeString& appendTo, 4712e5b6d6dSopenharmony_ci FieldPosition& pos, 4722e5b6d6dSopenharmony_ci UErrorCode& status) const override; 4732e5b6d6dSopenharmony_ci 4742e5b6d6dSopenharmony_ci /** 4752e5b6d6dSopenharmony_ci * Returns the pattern from applyPattern() or constructor(). 4762e5b6d6dSopenharmony_ci * 4772e5b6d6dSopenharmony_ci * @param appendTo output parameter to receive result. 4782e5b6d6dSopenharmony_ci * Result is appended to existing contents. 4792e5b6d6dSopenharmony_ci * @return the UnicodeString with inserted pattern. 4802e5b6d6dSopenharmony_ci * @stable ICU 4.0 4812e5b6d6dSopenharmony_ci */ 4822e5b6d6dSopenharmony_ci UnicodeString& toPattern(UnicodeString& appendTo); 4832e5b6d6dSopenharmony_ci 4842e5b6d6dSopenharmony_ci /** 4852e5b6d6dSopenharmony_ci * This method is not yet supported by <code>PluralFormat</code>. 4862e5b6d6dSopenharmony_ci * <P> 4872e5b6d6dSopenharmony_ci * Before calling, set parse_pos.index to the offset you want to start 4882e5b6d6dSopenharmony_ci * parsing at in the source. After calling, parse_pos.index is the end of 4892e5b6d6dSopenharmony_ci * the text you parsed. If error occurs, index is unchanged. 4902e5b6d6dSopenharmony_ci * <P> 4912e5b6d6dSopenharmony_ci * When parsing, leading whitespace is discarded (with a successful parse), 4922e5b6d6dSopenharmony_ci * while trailing whitespace is left as is. 4932e5b6d6dSopenharmony_ci * <P> 4942e5b6d6dSopenharmony_ci * See Format::parseObject() for more. 4952e5b6d6dSopenharmony_ci * 4962e5b6d6dSopenharmony_ci * @param source The string to be parsed into an object. 4972e5b6d6dSopenharmony_ci * @param result Formattable to be set to the parse result. 4982e5b6d6dSopenharmony_ci * If parse fails, return contents are undefined. 4992e5b6d6dSopenharmony_ci * @param parse_pos The position to start parsing at. Upon return 5002e5b6d6dSopenharmony_ci * this param is set to the position after the 5012e5b6d6dSopenharmony_ci * last character successfully parsed. If the 5022e5b6d6dSopenharmony_ci * source is not parsed successfully, this param 5032e5b6d6dSopenharmony_ci * will remain unchanged. 5042e5b6d6dSopenharmony_ci * @stable ICU 4.0 5052e5b6d6dSopenharmony_ci */ 5062e5b6d6dSopenharmony_ci virtual void parseObject(const UnicodeString& source, 5072e5b6d6dSopenharmony_ci Formattable& result, 5082e5b6d6dSopenharmony_ci ParsePosition& parse_pos) const override; 5092e5b6d6dSopenharmony_ci 5102e5b6d6dSopenharmony_ci /** 5112e5b6d6dSopenharmony_ci * ICU "poor man's RTTI", returns a UClassID for this class. 5122e5b6d6dSopenharmony_ci * 5132e5b6d6dSopenharmony_ci * @stable ICU 4.0 5142e5b6d6dSopenharmony_ci * 5152e5b6d6dSopenharmony_ci */ 5162e5b6d6dSopenharmony_ci static UClassID U_EXPORT2 getStaticClassID(void); 5172e5b6d6dSopenharmony_ci 5182e5b6d6dSopenharmony_ci /** 5192e5b6d6dSopenharmony_ci * ICU "poor man's RTTI", returns a UClassID for the actual class. 5202e5b6d6dSopenharmony_ci * 5212e5b6d6dSopenharmony_ci * @stable ICU 4.0 5222e5b6d6dSopenharmony_ci */ 5232e5b6d6dSopenharmony_ci virtual UClassID getDynamicClassID() const override; 5242e5b6d6dSopenharmony_ci 5252e5b6d6dSopenharmony_ciprivate: 5262e5b6d6dSopenharmony_ci /** 5272e5b6d6dSopenharmony_ci * @internal (private) 5282e5b6d6dSopenharmony_ci */ 5292e5b6d6dSopenharmony_ci class U_I18N_API PluralSelector : public UMemory { 5302e5b6d6dSopenharmony_ci public: 5312e5b6d6dSopenharmony_ci virtual ~PluralSelector(); 5322e5b6d6dSopenharmony_ci /** 5332e5b6d6dSopenharmony_ci * Given a number, returns the appropriate PluralFormat keyword. 5342e5b6d6dSopenharmony_ci * 5352e5b6d6dSopenharmony_ci * @param context worker object for the selector. 5362e5b6d6dSopenharmony_ci * @param number The number to be plural-formatted. 5372e5b6d6dSopenharmony_ci * @param ec Error code. 5382e5b6d6dSopenharmony_ci * @return The selected PluralFormat keyword. 5392e5b6d6dSopenharmony_ci * @internal (private) 5402e5b6d6dSopenharmony_ci */ 5412e5b6d6dSopenharmony_ci virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0; 5422e5b6d6dSopenharmony_ci }; 5432e5b6d6dSopenharmony_ci 5442e5b6d6dSopenharmony_ci class U_I18N_API PluralSelectorAdapter : public PluralSelector { 5452e5b6d6dSopenharmony_ci public: 5462e5b6d6dSopenharmony_ci PluralSelectorAdapter() : pluralRules(NULL) { 5472e5b6d6dSopenharmony_ci } 5482e5b6d6dSopenharmony_ci 5492e5b6d6dSopenharmony_ci virtual ~PluralSelectorAdapter(); 5502e5b6d6dSopenharmony_ci 5512e5b6d6dSopenharmony_ci virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const override; 5522e5b6d6dSopenharmony_ci 5532e5b6d6dSopenharmony_ci void reset(); 5542e5b6d6dSopenharmony_ci 5552e5b6d6dSopenharmony_ci PluralRules* pluralRules; 5562e5b6d6dSopenharmony_ci }; 5572e5b6d6dSopenharmony_ci 5582e5b6d6dSopenharmony_ci Locale locale; 5592e5b6d6dSopenharmony_ci MessagePattern msgPattern; 5602e5b6d6dSopenharmony_ci NumberFormat* numberFormat; 5612e5b6d6dSopenharmony_ci double offset; 5622e5b6d6dSopenharmony_ci PluralSelectorAdapter pluralRulesWrapper; 5632e5b6d6dSopenharmony_ci 5642e5b6d6dSopenharmony_ci PluralFormat() = delete; // default constructor not implemented 5652e5b6d6dSopenharmony_ci void init(const PluralRules* rules, UPluralType type, UErrorCode& status); 5662e5b6d6dSopenharmony_ci /** 5672e5b6d6dSopenharmony_ci * Copies dynamically allocated values (pointer fields). 5682e5b6d6dSopenharmony_ci * Others are copied using their copy constructors and assignment operators. 5692e5b6d6dSopenharmony_ci */ 5702e5b6d6dSopenharmony_ci void copyObjects(const PluralFormat& other); 5712e5b6d6dSopenharmony_ci 5722e5b6d6dSopenharmony_ci UnicodeString& format(const Formattable& numberObject, double number, 5732e5b6d6dSopenharmony_ci UnicodeString& appendTo, 5742e5b6d6dSopenharmony_ci FieldPosition& pos, 5752e5b6d6dSopenharmony_ci UErrorCode& status) const; 5762e5b6d6dSopenharmony_ci 5772e5b6d6dSopenharmony_ci /** 5782e5b6d6dSopenharmony_ci * Finds the PluralFormat sub-message for the given number, or the "other" sub-message. 5792e5b6d6dSopenharmony_ci * @param pattern A MessagePattern. 5802e5b6d6dSopenharmony_ci * @param partIndex the index of the first PluralFormat argument style part. 5812e5b6d6dSopenharmony_ci * @param selector the PluralSelector for mapping the number (minus offset) to a keyword. 5822e5b6d6dSopenharmony_ci * @param context worker object for the selector. 5832e5b6d6dSopenharmony_ci * @param number a number to be matched to one of the PluralFormat argument's explicit values, 5842e5b6d6dSopenharmony_ci * or mapped via the PluralSelector. 5852e5b6d6dSopenharmony_ci * @param ec ICU error code. 5862e5b6d6dSopenharmony_ci * @return the sub-message start part index. 5872e5b6d6dSopenharmony_ci */ 5882e5b6d6dSopenharmony_ci static int32_t findSubMessage( 5892e5b6d6dSopenharmony_ci const MessagePattern& pattern, int32_t partIndex, 5902e5b6d6dSopenharmony_ci const PluralSelector& selector, void *context, double number, UErrorCode& ec); 5912e5b6d6dSopenharmony_ci 5922e5b6d6dSopenharmony_ci void parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner, 5932e5b6d6dSopenharmony_ci Formattable& result, FieldPosition& pos) const; 5942e5b6d6dSopenharmony_ci 5952e5b6d6dSopenharmony_ci friend class MessageFormat; 5962e5b6d6dSopenharmony_ci friend class NFRule; 5972e5b6d6dSopenharmony_ci}; 5982e5b6d6dSopenharmony_ci 5992e5b6d6dSopenharmony_ciU_NAMESPACE_END 6002e5b6d6dSopenharmony_ci 6012e5b6d6dSopenharmony_ci#endif /* #if !UCONFIG_NO_FORMATTING */ 6022e5b6d6dSopenharmony_ci 6032e5b6d6dSopenharmony_ci#endif /* U_SHOW_CPLUSPLUS_API */ 6042e5b6d6dSopenharmony_ci 6052e5b6d6dSopenharmony_ci#endif // _PLURFMT 6062e5b6d6dSopenharmony_ci//eof 607