|
||||
Warning, file /include/unicode/plurfmt.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ******************************************************************************* 0005 * Copyright (C) 2007-2014, International Business Machines Corporation and 0006 * others. All Rights Reserved. 0007 ******************************************************************************* 0008 * 0009 0010 * File PLURFMT.H 0011 ******************************************************************************** 0012 */ 0013 0014 #ifndef PLURFMT 0015 #define PLURFMT 0016 0017 #include "unicode/utypes.h" 0018 0019 #if U_SHOW_CPLUSPLUS_API 0020 0021 /** 0022 * \file 0023 * \brief C++ API: PluralFormat object 0024 */ 0025 0026 #if !UCONFIG_NO_FORMATTING 0027 0028 #include "unicode/messagepattern.h" 0029 #include "unicode/numfmt.h" 0030 #include "unicode/plurrule.h" 0031 0032 U_NAMESPACE_BEGIN 0033 0034 class Hashtable; 0035 class NFRule; 0036 0037 /** 0038 * <p> 0039 * <code>PluralFormat</code> supports the creation of internationalized 0040 * messages with plural inflection. It is based on <i>plural 0041 * selection</i>, i.e. the caller specifies messages for each 0042 * plural case that can appear in the user's language and the 0043 * <code>PluralFormat</code> selects the appropriate message based on 0044 * the number. 0045 * </p> 0046 * <h4>The Problem of Plural Forms in Internationalized Messages</h4> 0047 * <p> 0048 * Different languages have different ways to inflect 0049 * plurals. Creating internationalized messages that include plural 0050 * forms is only feasible when the framework is able to handle plural 0051 * forms of <i>all</i> languages correctly. <code>ChoiceFormat</code> 0052 * doesn't handle this well, because it attaches a number interval to 0053 * each message and selects the message whose interval contains a 0054 * given number. This can only handle a finite number of 0055 * intervals. But in some languages, like Polish, one plural case 0056 * applies to infinitely many intervals (e.g., the plural case applies to 0057 * numbers ending with 2, 3, or 4 except those ending with 12, 13, or 0058 * 14). Thus <code>ChoiceFormat</code> is not adequate. 0059 * </p><p> 0060 * <code>PluralFormat</code> deals with this by breaking the problem 0061 * into two parts: 0062 * <ul> 0063 * <li>It uses <code>PluralRules</code> that can define more complex 0064 * conditions for a plural case than just a single interval. These plural 0065 * rules define both what plural cases exist in a language, and to 0066 * which numbers these cases apply. 0067 * <li>It provides predefined plural rules for many languages. Thus, the programmer 0068 * need not worry about the plural cases of a language and 0069 * does not have to define the plural cases; they can simply 0070 * use the predefined keywords. The whole plural formatting of messages can 0071 * be done using localized patterns from resource bundles. For predefined plural 0072 * rules, see the CLDR <i>Language Plural Rules</i> page at 0073 * https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html 0074 * </ul> 0075 * </p> 0076 * <h4>Usage of <code>PluralFormat</code></h4> 0077 * <p>Note: Typically, plural formatting is done via <code>MessageFormat</code> 0078 * with a <code>plural</code> argument type, 0079 * rather than using a stand-alone <code>PluralFormat</code>. 0080 * </p><p> 0081 * This discussion assumes that you use <code>PluralFormat</code> with 0082 * a predefined set of plural rules. You can create one using one of 0083 * the constructors that takes a <code>locale</code> object. To 0084 * specify the message pattern, you can either pass it to the 0085 * constructor or set it explicitly using the 0086 * <code>applyPattern()</code> method. The <code>format()</code> 0087 * method takes a number object and selects the message of the 0088 * matching plural case. This message will be returned. 0089 * </p> 0090 * <h5>Patterns and Their Interpretation</h5> 0091 * <p> 0092 * The pattern text defines the message output for each plural case of the 0093 * specified locale. Syntax: 0094 * <pre> 0095 * pluralStyle = [offsetValue] (selector '{' message '}')+ 0096 * offsetValue = "offset:" number 0097 * selector = explicitValue | keyword 0098 * explicitValue = '=' number // adjacent, no white space in between 0099 * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ 0100 * message: see {@link MessageFormat} 0101 * </pre> 0102 * Pattern_White_Space between syntax elements is ignored, except 0103 * between the {curly braces} and their sub-message, 0104 * and between the '=' and the number of an explicitValue. 0105 * 0106 * </p><p> 0107 * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and 0108 * 'other'. You always have to define a message text for the default plural case 0109 * <code>other</code> which is contained in every rule set. 0110 * If you do not specify a message text for a particular plural case, the 0111 * message text of the plural case <code>other</code> gets assigned to this 0112 * plural case. 0113 * </p><p> 0114 * When formatting, the input number is first matched against the explicitValue clauses. 0115 * If there is no exact-number match, then a keyword is selected by calling 0116 * the <code>PluralRules</code> with the input number <em>minus the offset</em>. 0117 * (The offset defaults to 0 if it is omitted from the pattern string.) 0118 * If there is no clause with that keyword, then the "other" clauses is returned. 0119 * </p><p> 0120 * An unquoted pound sign (<code>#</code>) in the selected sub-message 0121 * itself (i.e., outside of arguments nested in the sub-message) 0122 * is replaced by the input number minus the offset. 0123 * The number-minus-offset value is formatted using a 0124 * <code>NumberFormat</code> for the <code>PluralFormat</code>'s locale. If you 0125 * need special number formatting, you have to use a <code>MessageFormat</code> 0126 * and explicitly specify a <code>NumberFormat</code> argument. 0127 * <strong>Note:</strong> That argument is formatting without subtracting the offset! 0128 * If you need a custom format and have a non-zero offset, then you need to pass the 0129 * number-minus-offset value as a separate parameter. 0130 * </p> 0131 * For a usage example, see the {@link MessageFormat} class documentation. 0132 * 0133 * <h4>Defining Custom Plural Rules</h4> 0134 * <p>If you need to use <code>PluralFormat</code> with custom rules, you can 0135 * create a <code>PluralRules</code> object and pass it to 0136 * <code>PluralFormat</code>'s constructor. If you also specify a locale in this 0137 * constructor, this locale will be used to format the number in the message 0138 * texts. 0139 * </p><p> 0140 * For more information about <code>PluralRules</code>, see 0141 * {@link PluralRules}. 0142 * </p> 0143 * 0144 * ported from Java 0145 * @stable ICU 4.0 0146 */ 0147 0148 class U_I18N_API PluralFormat : public Format { 0149 public: 0150 0151 /** 0152 * Creates a new cardinal-number <code>PluralFormat</code> for the default locale. 0153 * This locale will be used to get the set of plural rules and for standard 0154 * number formatting. 0155 * @param status output param set to success/failure code on exit, which 0156 * must not indicate a failure before the function call. 0157 * @stable ICU 4.0 0158 */ 0159 PluralFormat(UErrorCode& status); 0160 0161 /** 0162 * Creates a new cardinal-number <code>PluralFormat</code> for a given locale. 0163 * @param locale the <code>PluralFormat</code> will be configured with 0164 * rules for this locale. This locale will also be used for 0165 * standard number formatting. 0166 * @param status output param set to success/failure code on exit, which 0167 * must not indicate a failure before the function call. 0168 * @stable ICU 4.0 0169 */ 0170 PluralFormat(const Locale& locale, UErrorCode& status); 0171 0172 /** 0173 * Creates a new <code>PluralFormat</code> for a given set of rules. 0174 * The standard number formatting will be done using the default locale. 0175 * @param rules defines the behavior of the <code>PluralFormat</code> 0176 * object. 0177 * @param status output param set to success/failure code on exit, which 0178 * must not indicate a failure before the function call. 0179 * @stable ICU 4.0 0180 */ 0181 PluralFormat(const PluralRules& rules, UErrorCode& status); 0182 0183 /** 0184 * Creates a new <code>PluralFormat</code> for a given set of rules. 0185 * The standard number formatting will be done using the given locale. 0186 * @param locale the default number formatting will be done using this 0187 * locale. 0188 * @param rules defines the behavior of the <code>PluralFormat</code> 0189 * object. 0190 * @param status output param set to success/failure code on exit, which 0191 * must not indicate a failure before the function call. 0192 * @stable ICU 4.0 0193 * <p> 0194 * <h4>Sample code</h4> 0195 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample1 0196 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample 0197 * <p> 0198 */ 0199 PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status); 0200 0201 /** 0202 * Creates a new <code>PluralFormat</code> for the plural type. 0203 * The standard number formatting will be done using the given locale. 0204 * @param locale the default number formatting will be done using this 0205 * locale. 0206 * @param type The plural type (e.g., cardinal or ordinal). 0207 * @param status output param set to success/failure code on exit, which 0208 * must not indicate a failure before the function call. 0209 * @stable ICU 50 0210 */ 0211 PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status); 0212 0213 /** 0214 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string. 0215 * The default locale will be used to get the set of plural rules and for 0216 * standard number formatting. 0217 * @param pattern the pattern for this <code>PluralFormat</code>. 0218 * errors are returned to status if the pattern is invalid. 0219 * @param status output param set to success/failure code on exit, which 0220 * must not indicate a failure before the function call. 0221 * @stable ICU 4.0 0222 */ 0223 PluralFormat(const UnicodeString& pattern, UErrorCode& status); 0224 0225 /** 0226 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string and 0227 * locale. 0228 * The locale will be used to get the set of plural rules and for 0229 * standard number formatting. 0230 * @param locale the <code>PluralFormat</code> will be configured with 0231 * rules for this locale. This locale will also be used for 0232 * standard number formatting. 0233 * @param pattern the pattern for this <code>PluralFormat</code>. 0234 * errors are returned to status if the pattern is invalid. 0235 * @param status output param set to success/failure code on exit, which 0236 * must not indicate a failure before the function call. 0237 * @stable ICU 4.0 0238 */ 0239 PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status); 0240 0241 /** 0242 * Creates a new <code>PluralFormat</code> for a given set of rules, a 0243 * pattern and a locale. 0244 * @param rules defines the behavior of the <code>PluralFormat</code> 0245 * object. 0246 * @param pattern the pattern for this <code>PluralFormat</code>. 0247 * errors are returned to status if the pattern is invalid. 0248 * @param status output param set to success/failure code on exit, which 0249 * must not indicate a failure before the function call. 0250 * @stable ICU 4.0 0251 */ 0252 PluralFormat(const PluralRules& rules, 0253 const UnicodeString& pattern, 0254 UErrorCode& status); 0255 0256 /** 0257 * Creates a new <code>PluralFormat</code> for a given set of rules, a 0258 * pattern and a locale. 0259 * @param locale the <code>PluralFormat</code> will be configured with 0260 * rules for this locale. This locale will also be used for 0261 * standard number formatting. 0262 * @param rules defines the behavior of the <code>PluralFormat</code> 0263 * object. 0264 * @param pattern the pattern for this <code>PluralFormat</code>. 0265 * errors are returned to status if the pattern is invalid. 0266 * @param status output param set to success/failure code on exit, which 0267 * must not indicate a failure before the function call. 0268 * @stable ICU 4.0 0269 */ 0270 PluralFormat(const Locale& locale, 0271 const PluralRules& rules, 0272 const UnicodeString& pattern, 0273 UErrorCode& status); 0274 0275 /** 0276 * Creates a new <code>PluralFormat</code> for a plural type, a 0277 * pattern and a locale. 0278 * @param locale the <code>PluralFormat</code> will be configured with 0279 * rules for this locale. This locale will also be used for 0280 * standard number formatting. 0281 * @param type The plural type (e.g., cardinal or ordinal). 0282 * @param pattern the pattern for this <code>PluralFormat</code>. 0283 * errors are returned to status if the pattern is invalid. 0284 * @param status output param set to success/failure code on exit, which 0285 * must not indicate a failure before the function call. 0286 * @stable ICU 50 0287 */ 0288 PluralFormat(const Locale& locale, 0289 UPluralType type, 0290 const UnicodeString& pattern, 0291 UErrorCode& status); 0292 0293 /** 0294 * copy constructor. 0295 * @stable ICU 4.0 0296 */ 0297 PluralFormat(const PluralFormat& other); 0298 0299 /** 0300 * Destructor. 0301 * @stable ICU 4.0 0302 */ 0303 virtual ~PluralFormat(); 0304 0305 /** 0306 * Sets the pattern used by this plural format. 0307 * The method parses the pattern and creates a map of format strings 0308 * for the plural rules. 0309 * Patterns and their interpretation are specified in the class description. 0310 * 0311 * @param pattern the pattern for this plural format 0312 * errors are returned to status if the pattern is invalid. 0313 * @param status output param set to success/failure code on exit, which 0314 * must not indicate a failure before the function call. 0315 * @stable ICU 4.0 0316 */ 0317 void applyPattern(const UnicodeString& pattern, UErrorCode& status); 0318 0319 0320 using Format::format; 0321 0322 /** 0323 * Formats a plural message for a given number. 0324 * 0325 * @param number a number for which the plural message should be formatted 0326 * for. If no pattern has been applied to this 0327 * <code>PluralFormat</code> object yet, the formatted number 0328 * will be returned. 0329 * @param status output param set to success/failure code on exit, which 0330 * must not indicate a failure before the function call. 0331 * @return the string containing the formatted plural message. 0332 * @stable ICU 4.0 0333 */ 0334 UnicodeString format(int32_t number, UErrorCode& status) const; 0335 0336 /** 0337 * Formats a plural message for a given number. 0338 * 0339 * @param number a number for which the plural message should be formatted 0340 * for. If no pattern has been applied to this 0341 * PluralFormat object yet, the formatted number 0342 * will be returned. 0343 * @param status output param set to success or failure code on exit, which 0344 * must not indicate a failure before the function call. 0345 * @return the string containing the formatted plural message. 0346 * @stable ICU 4.0 0347 */ 0348 UnicodeString format(double number, UErrorCode& status) const; 0349 0350 /** 0351 * Formats a plural message for a given number. 0352 * 0353 * @param number a number for which the plural message should be formatted 0354 * for. If no pattern has been applied to this 0355 * <code>PluralFormat</code> object yet, the formatted number 0356 * will be returned. 0357 * @param appendTo output parameter to receive result. 0358 * result is appended to existing contents. 0359 * @param pos On input: an alignment field, if desired. 0360 * On output: the offsets of the alignment field. 0361 * @param status output param set to success/failure code on exit, which 0362 * must not indicate a failure before the function call. 0363 * @return the string containing the formatted plural message. 0364 * @stable ICU 4.0 0365 */ 0366 UnicodeString& format(int32_t number, 0367 UnicodeString& appendTo, 0368 FieldPosition& pos, 0369 UErrorCode& status) const; 0370 0371 /** 0372 * Formats a plural message for a given number. 0373 * 0374 * @param number a number for which the plural message should be formatted 0375 * for. If no pattern has been applied to this 0376 * PluralFormat object yet, the formatted number 0377 * will be returned. 0378 * @param appendTo output parameter to receive result. 0379 * result is appended to existing contents. 0380 * @param pos On input: an alignment field, if desired. 0381 * On output: the offsets of the alignment field. 0382 * @param status output param set to success/failure code on exit, which 0383 * must not indicate a failure before the function call. 0384 * @return the string containing the formatted plural message. 0385 * @stable ICU 4.0 0386 */ 0387 UnicodeString& format(double number, 0388 UnicodeString& appendTo, 0389 FieldPosition& pos, 0390 UErrorCode& status) const; 0391 0392 #ifndef U_HIDE_DEPRECATED_API 0393 /** 0394 * Sets the locale used by this <code>PluraFormat</code> object. 0395 * Note: Calling this method resets this <code>PluraFormat</code> object, 0396 * i.e., a pattern that was applied previously will be removed, 0397 * and the NumberFormat is set to the default number format for 0398 * the locale. The resulting format behaves the same as one 0399 * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)} 0400 * with UPLURAL_TYPE_CARDINAL. 0401 * @param locale the <code>locale</code> to use to configure the formatter. 0402 * @param status output param set to success/failure code on exit, which 0403 * must not indicate a failure before the function call. 0404 * @deprecated ICU 50 This method clears the pattern and might create 0405 * a different kind of PluralRules instance; 0406 * use one of the constructors to create a new instance instead. 0407 */ 0408 void setLocale(const Locale& locale, UErrorCode& status); 0409 #endif /* U_HIDE_DEPRECATED_API */ 0410 0411 /** 0412 * Sets the number format used by this formatter. You only need to 0413 * call this if you want a different number format than the default 0414 * formatter for the locale. 0415 * @param format the number format to use. 0416 * @param status output param set to success/failure code on exit, which 0417 * must not indicate a failure before the function call. 0418 * @stable ICU 4.0 0419 */ 0420 void setNumberFormat(const NumberFormat* format, UErrorCode& status); 0421 0422 /** 0423 * Assignment operator 0424 * 0425 * @param other the PluralFormat object to copy from. 0426 * @stable ICU 4.0 0427 */ 0428 PluralFormat& operator=(const PluralFormat& other); 0429 0430 /** 0431 * Return true if another object is semantically equal to this one. 0432 * 0433 * @param other the PluralFormat object to be compared with. 0434 * @return true if other is semantically equal to this. 0435 * @stable ICU 4.0 0436 */ 0437 virtual bool operator==(const Format& other) const override; 0438 0439 /** 0440 * Return true if another object is semantically unequal to this one. 0441 * 0442 * @param other the PluralFormat object to be compared with. 0443 * @return true if other is semantically unequal to this. 0444 * @stable ICU 4.0 0445 */ 0446 virtual bool operator!=(const Format& other) const; 0447 0448 /** 0449 * Clones this Format object polymorphically. The caller owns the 0450 * result and should delete it when done. 0451 * @stable ICU 4.0 0452 */ 0453 virtual PluralFormat* clone() const override; 0454 0455 /** 0456 * Formats a plural message for a number taken from a Formattable object. 0457 * 0458 * @param obj The object containing a number for which the 0459 * plural message should be formatted. 0460 * The object must be of a numeric type. 0461 * @param appendTo output parameter to receive result. 0462 * Result is appended to existing contents. 0463 * @param pos On input: an alignment field, if desired. 0464 * On output: the offsets of the alignment field. 0465 * @param status output param filled with success/failure status. 0466 * @return Reference to 'appendTo' parameter. 0467 * @stable ICU 4.0 0468 */ 0469 UnicodeString& format(const Formattable& obj, 0470 UnicodeString& appendTo, 0471 FieldPosition& pos, 0472 UErrorCode& status) const override; 0473 0474 /** 0475 * Returns the pattern from applyPattern() or constructor(). 0476 * 0477 * @param appendTo output parameter to receive result. 0478 * Result is appended to existing contents. 0479 * @return the UnicodeString with inserted pattern. 0480 * @stable ICU 4.0 0481 */ 0482 UnicodeString& toPattern(UnicodeString& appendTo); 0483 0484 /** 0485 * This method is not yet supported by <code>PluralFormat</code>. 0486 * <P> 0487 * Before calling, set parse_pos.index to the offset you want to start 0488 * parsing at in the source. After calling, parse_pos.index is the end of 0489 * the text you parsed. If error occurs, index is unchanged. 0490 * <P> 0491 * When parsing, leading whitespace is discarded (with a successful parse), 0492 * while trailing whitespace is left as is. 0493 * <P> 0494 * See Format::parseObject() for more. 0495 * 0496 * @param source The string to be parsed into an object. 0497 * @param result Formattable to be set to the parse result. 0498 * If parse fails, return contents are undefined. 0499 * @param parse_pos The position to start parsing at. Upon return 0500 * this param is set to the position after the 0501 * last character successfully parsed. If the 0502 * source is not parsed successfully, this param 0503 * will remain unchanged. 0504 * @stable ICU 4.0 0505 */ 0506 virtual void parseObject(const UnicodeString& source, 0507 Formattable& result, 0508 ParsePosition& parse_pos) const override; 0509 0510 /** 0511 * ICU "poor man's RTTI", returns a UClassID for this class. 0512 * 0513 * @stable ICU 4.0 0514 * 0515 */ 0516 static UClassID U_EXPORT2 getStaticClassID(void); 0517 0518 /** 0519 * ICU "poor man's RTTI", returns a UClassID for the actual class. 0520 * 0521 * @stable ICU 4.0 0522 */ 0523 virtual UClassID getDynamicClassID() const override; 0524 0525 private: 0526 /** 0527 * @internal (private) 0528 */ 0529 class U_I18N_API PluralSelector : public UMemory { 0530 public: 0531 virtual ~PluralSelector(); 0532 /** 0533 * Given a number, returns the appropriate PluralFormat keyword. 0534 * 0535 * @param context worker object for the selector. 0536 * @param number The number to be plural-formatted. 0537 * @param ec Error code. 0538 * @return The selected PluralFormat keyword. 0539 * @internal (private) 0540 */ 0541 virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0; 0542 }; 0543 0544 class U_I18N_API PluralSelectorAdapter : public PluralSelector { 0545 public: 0546 PluralSelectorAdapter() : pluralRules(nullptr) { 0547 } 0548 0549 virtual ~PluralSelectorAdapter(); 0550 0551 virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const override; 0552 0553 void reset(); 0554 0555 PluralRules* pluralRules; 0556 }; 0557 0558 Locale locale; 0559 MessagePattern msgPattern; 0560 NumberFormat* numberFormat; 0561 double offset; 0562 PluralSelectorAdapter pluralRulesWrapper; 0563 0564 PluralFormat() = delete; // default constructor not implemented 0565 void init(const PluralRules* rules, UPluralType type, UErrorCode& status); 0566 /** 0567 * Copies dynamically allocated values (pointer fields). 0568 * Others are copied using their copy constructors and assignment operators. 0569 */ 0570 void copyObjects(const PluralFormat& other); 0571 0572 UnicodeString& format(const Formattable& numberObject, double number, 0573 UnicodeString& appendTo, 0574 FieldPosition& pos, 0575 UErrorCode& status) const; 0576 0577 /** 0578 * Finds the PluralFormat sub-message for the given number, or the "other" sub-message. 0579 * @param pattern A MessagePattern. 0580 * @param partIndex the index of the first PluralFormat argument style part. 0581 * @param selector the PluralSelector for mapping the number (minus offset) to a keyword. 0582 * @param context worker object for the selector. 0583 * @param number a number to be matched to one of the PluralFormat argument's explicit values, 0584 * or mapped via the PluralSelector. 0585 * @param ec ICU error code. 0586 * @return the sub-message start part index. 0587 */ 0588 static int32_t findSubMessage( 0589 const MessagePattern& pattern, int32_t partIndex, 0590 const PluralSelector& selector, void *context, double number, UErrorCode& ec); 0591 0592 void parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner, 0593 Formattable& result, FieldPosition& pos) const; 0594 0595 friend class MessageFormat; 0596 friend class NFRule; 0597 }; 0598 0599 U_NAMESPACE_END 0600 0601 #endif /* #if !UCONFIG_NO_FORMATTING */ 0602 0603 #endif /* U_SHOW_CPLUSPLUS_API */ 0604 0605 #endif // _PLURFMT 0606 //eof
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |