|
||||
Warning, file /include/unicode/msgfmt.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 * Copyright (C) 2007-2013, International Business Machines Corporation and 0005 * others. All Rights Reserved. 0006 ******************************************************************************** 0007 * 0008 * File MSGFMT.H 0009 * 0010 * Modification History: 0011 * 0012 * Date Name Description 0013 * 02/19/97 aliu Converted from java. 0014 * 03/20/97 helena Finished first cut of implementation. 0015 * 07/22/98 stephen Removed operator!= (defined in Format) 0016 * 08/19/2002 srl Removing Javaisms 0017 *******************************************************************************/ 0018 0019 #ifndef MSGFMT_H 0020 #define MSGFMT_H 0021 0022 #include "unicode/utypes.h" 0023 0024 #if U_SHOW_CPLUSPLUS_API 0025 0026 /** 0027 * \file 0028 * \brief C++ API: Formats messages in a language-neutral way. 0029 */ 0030 0031 #if !UCONFIG_NO_FORMATTING 0032 0033 #include "unicode/format.h" 0034 #include "unicode/locid.h" 0035 #include "unicode/messagepattern.h" 0036 #include "unicode/parseerr.h" 0037 #include "unicode/plurfmt.h" 0038 #include "unicode/plurrule.h" 0039 0040 U_CDECL_BEGIN 0041 // Forward declaration. 0042 struct UHashtable; 0043 typedef struct UHashtable UHashtable; /**< @internal */ 0044 U_CDECL_END 0045 0046 U_NAMESPACE_BEGIN 0047 0048 class AppendableWrapper; 0049 class DateFormat; 0050 class NumberFormat; 0051 0052 /** 0053 * <p>MessageFormat prepares strings for display to users, 0054 * with optional arguments (variables/placeholders). 0055 * The arguments can occur in any order, which is necessary for translation 0056 * into languages with different grammars. 0057 * 0058 * <p>A MessageFormat is constructed from a <em>pattern</em> string 0059 * with arguments in {curly braces} which will be replaced by formatted values. 0060 * 0061 * <p><code>MessageFormat</code> differs from the other <code>Format</code> 0062 * classes in that you create a <code>MessageFormat</code> object with one 0063 * of its constructors (not with a <code>createInstance</code> style factory 0064 * method). Factory methods aren't necessary because <code>MessageFormat</code> 0065 * itself doesn't implement locale-specific behavior. Any locale-specific 0066 * behavior is defined by the pattern that you provide and the 0067 * subformats used for inserted arguments. 0068 * 0069 * <p>Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers). 0070 * Some of the API methods work only with argument numbers and throw an exception 0071 * if the pattern has named arguments (see {@link #usesNamedArguments()}). 0072 * 0073 * <p>An argument might not specify any format type. In this case, 0074 * a numeric value is formatted with a default (for the locale) NumberFormat, 0075 * and a date/time value is formatted with a default (for the locale) DateFormat. 0076 * 0077 * <p>An argument might specify a "simple" type for which the specified 0078 * Format object is created, cached and used. 0079 * 0080 * <p>An argument might have a "complex" type with nested MessageFormat sub-patterns. 0081 * During formatting, one of these sub-messages is selected according to the argument value 0082 * and recursively formatted. 0083 * 0084 * <p>After construction, a custom Format object can be set for 0085 * a top-level argument, overriding the default formatting and parsing behavior 0086 * for that argument. 0087 * However, custom formatting can be achieved more simply by writing 0088 * a typeless argument in the pattern string 0089 * and supplying it with a preformatted string value. 0090 * 0091 * <p>When formatting, MessageFormat takes a collection of argument values 0092 * and writes an output string. 0093 * The argument values may be passed as an array 0094 * (when the pattern contains only numbered arguments) 0095 * or as an array of names and and an array of arguments (which works for both named 0096 * and numbered arguments). 0097 * 0098 * <p>Each argument is matched with one of the input values by array index or argument name 0099 * and formatted according to its pattern specification 0100 * (or using a custom Format object if one was set). 0101 * A numbered pattern argument is matched with an argument name that contains that number 0102 * as an ASCII-decimal-digit string (without leading zero). 0103 * 0104 * <h4><a name="patterns">Patterns and Their Interpretation</a></h4> 0105 * 0106 * <code>MessageFormat</code> uses patterns of the following form: 0107 * <pre> 0108 * message = messageText (argument messageText)* 0109 * argument = noneArg | simpleArg | complexArg 0110 * complexArg = choiceArg | pluralArg | selectArg | selectordinalArg 0111 * 0112 * noneArg = '{' argNameOrNumber '}' 0113 * simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}' 0114 * choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}' 0115 * pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}' 0116 * selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}' 0117 * selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}' 0118 * 0119 * choiceStyle: see {@link ChoiceFormat} 0120 * pluralStyle: see {@link PluralFormat} 0121 * selectStyle: see {@link SelectFormat} 0122 * 0123 * argNameOrNumber = argName | argNumber 0124 * argName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ 0125 * argNumber = '0' | ('1'..'9' ('0'..'9')*) 0126 * 0127 * argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration" 0128 * argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText | "::" argSkeletonText 0129 * </pre> 0130 * 0131 * <ul> 0132 * <li>messageText can contain quoted literal strings including syntax characters. 0133 * A quoted literal string begins with an ASCII apostrophe and a syntax character 0134 * (usually a {curly brace}) and continues until the next single apostrophe. 0135 * A double ASCII apostrophe inside or outside of a quoted string represents 0136 * one literal apostrophe. 0137 * <li>Quotable syntax characters are the {curly braces} in all messageText parts, 0138 * plus the '#' sign in a messageText immediately inside a pluralStyle, 0139 * and the '|' symbol in a messageText immediately inside a choiceStyle. 0140 * <li>See also {@link #UMessagePatternApostropheMode} 0141 * <li>In argStyleText, every single ASCII apostrophe begins and ends quoted literal text, 0142 * and unquoted {curly braces} must occur in matched pairs. 0143 * </ul> 0144 * 0145 * <p>Recommendation: Use the real apostrophe (single quote) character 0146 * \htmlonly’\endhtmlonly (U+2019) for 0147 * human-readable text, and use the ASCII apostrophe ' (U+0027) 0148 * only in program syntax, like quoting in MessageFormat. 0149 * See the annotations for U+0027 Apostrophe in The Unicode Standard. 0150 * 0151 * <p>The <code>choice</code> argument type is deprecated. 0152 * Use <code>plural</code> arguments for proper plural selection, 0153 * and <code>select</code> arguments for simple selection among a fixed set of choices. 0154 * 0155 * <p>The <code>argType</code> and <code>argStyle</code> values are used to create 0156 * a <code>Format</code> instance for the format element. The following 0157 * table shows how the values map to Format instances. Combinations not 0158 * shown in the table are illegal. Any <code>argStyleText</code> must 0159 * be a valid pattern string for the Format subclass used. 0160 * 0161 * <p><table border=1> 0162 * <tr> 0163 * <th>argType 0164 * <th>argStyle 0165 * <th>resulting Format object 0166 * <tr> 0167 * <td colspan=2><i>(none)</i> 0168 * <td><code>null</code> 0169 * <tr> 0170 * <td rowspan=6><code>number</code> 0171 * <td><i>(none)</i> 0172 * <td><code>NumberFormat.createInstance(getLocale(), status)</code> 0173 * <tr> 0174 * <td><code>integer</code> 0175 * <td><code>NumberFormat.createInstance(getLocale(), kNumberStyle, status)</code> 0176 * <tr> 0177 * <td><code>currency</code> 0178 * <td><code>NumberFormat.createCurrencyInstance(getLocale(), status)</code> 0179 * <tr> 0180 * <td><code>percent</code> 0181 * <td><code>NumberFormat.createPercentInstance(getLocale(), status)</code> 0182 * <tr> 0183 * <td><i>argStyleText</i> 0184 * <td><code>new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status)</code> 0185 * <tr> 0186 * <td><i>argSkeletonText</i> 0187 * <td><code>NumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status)</code> 0188 * <tr> 0189 * <td rowspan=7><code>date</code> 0190 * <td><i>(none)</i> 0191 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code> 0192 * <tr> 0193 * <td><code>short</code> 0194 * <td><code>DateFormat.createDateInstance(kShort, getLocale(), status)</code> 0195 * <tr> 0196 * <td><code>medium</code> 0197 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code> 0198 * <tr> 0199 * <td><code>long</code> 0200 * <td><code>DateFormat.createDateInstance(kLong, getLocale(), status)</code> 0201 * <tr> 0202 * <td><code>full</code> 0203 * <td><code>DateFormat.createDateInstance(kFull, getLocale(), status)</code> 0204 * <tr> 0205 * <td><i>argStyleText</i> 0206 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code> 0207 * <tr> 0208 * <td><i>argSkeletonText</i> 0209 * <td><code>DateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status)</code> 0210 * <tr> 0211 * <td rowspan=6><code>time</code> 0212 * <td><i>(none)</i> 0213 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code> 0214 * <tr> 0215 * <td><code>short</code> 0216 * <td><code>DateFormat.createTimeInstance(kShort, getLocale(), status)</code> 0217 * <tr> 0218 * <td><code>medium</code> 0219 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code> 0220 * <tr> 0221 * <td><code>long</code> 0222 * <td><code>DateFormat.createTimeInstance(kLong, getLocale(), status)</code> 0223 * <tr> 0224 * <td><code>full</code> 0225 * <td><code>DateFormat.createTimeInstance(kFull, getLocale(), status)</code> 0226 * <tr> 0227 * <td><i>argStyleText</i> 0228 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code> 0229 * <tr> 0230 * <td><code>spellout</code> 0231 * <td><i>argStyleText (optional)</i> 0232 * <td><code>new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status) 0233 * <br/> .setDefaultRuleset(argStyleText, status);</code> 0234 * <tr> 0235 * <td><code>ordinal</code> 0236 * <td><i>argStyleText (optional)</i> 0237 * <td><code>new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status) 0238 * <br/> .setDefaultRuleset(argStyleText, status);</code> 0239 * <tr> 0240 * <td><code>duration</code> 0241 * <td><i>argStyleText (optional)</i> 0242 * <td><code>new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status) 0243 * <br/> .setDefaultRuleset(argStyleText, status);</code> 0244 * </table> 0245 * <p> 0246 * 0247 * <h4>Argument formatting</h4> 0248 * 0249 * <p>Arguments are formatted according to their type, using the default 0250 * ICU formatters for those types, unless otherwise specified.</p> 0251 * 0252 * <p>There are also several ways to control the formatting.</p> 0253 * 0254 * <p>We recommend you use default styles, predefined style values, skeletons, 0255 * or preformatted values, but not pattern strings or custom format objects.</p> 0256 * 0257 * <p>For more details, see the 0258 * <a href="https://unicode-org.github.io/icu/userguide/format_parse/messages">ICU User Guide</a>.</p> 0259 * 0260 * <h4>Usage Information</h4> 0261 * 0262 * <p>Here are some examples of usage: 0263 * Example 1: 0264 * 0265 * <pre> 0266 * \code 0267 * UErrorCode success = U_ZERO_ERROR; 0268 * GregorianCalendar cal(success); 0269 * Formattable arguments[] = { 0270 * 7L, 0271 * Formattable( (Date) cal.getTime(success), Formattable::kIsDate), 0272 * "a disturbance in the Force" 0273 * }; 0274 * 0275 * UnicodeString result; 0276 * MessageFormat::format( 0277 * "At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {0,number}.", 0278 * arguments, 3, result, success ); 0279 * 0280 * cout << "result: " << result << endl; 0281 * //<output>: At 4:34 PM on March 23, there was a disturbance 0282 * // in the Force on planet 7. 0283 * \endcode 0284 * </pre> 0285 * 0286 * Typically, the message format will come from resources, and the 0287 * arguments will be dynamically set at runtime. 0288 * 0289 * <p>Example 2: 0290 * 0291 * <pre> 0292 * \code 0293 * success = U_ZERO_ERROR; 0294 * Formattable testArgs[] = {3L, "MyDisk"}; 0295 * 0296 * MessageFormat form( 0297 * "The disk \"{1}\" contains {0} file(s).", success ); 0298 * 0299 * UnicodeString string; 0300 * FieldPosition fpos = 0; 0301 * cout << "format: " << form.format(testArgs, 2, string, fpos, success ) << endl; 0302 * 0303 * // output, with different testArgs: 0304 * // output: The disk "MyDisk" contains 0 file(s). 0305 * // output: The disk "MyDisk" contains 1 file(s). 0306 * // output: The disk "MyDisk" contains 1,273 file(s). 0307 * \endcode 0308 * </pre> 0309 * 0310 * 0311 * <p>For messages that include plural forms, you can use a plural argument: 0312 * <pre> 0313 * \code 0314 * success = U_ZERO_ERROR; 0315 * MessageFormat msgFmt( 0316 * "{num_files, plural, " 0317 * "=0{There are no files on disk \"{disk_name}\".}" 0318 * "=1{There is one file on disk \"{disk_name}\".}" 0319 * "other{There are # files on disk \"{disk_name}\".}}", 0320 * Locale("en"), 0321 * success); 0322 * FieldPosition fpos = 0; 0323 * Formattable testArgs[] = {0L, "MyDisk"}; 0324 * UnicodeString testArgsNames[] = {"num_files", "disk_name"}; 0325 * UnicodeString result; 0326 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success); 0327 * testArgs[0] = 3L; 0328 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success); 0329 * \endcode 0330 * <em>output</em>: 0331 * There are no files on disk "MyDisk". 0332 * There are 3 files on "MyDisk". 0333 * </pre> 0334 * See {@link PluralFormat} and {@link PluralRules} for details. 0335 * 0336 * <h4><a name="synchronization">Synchronization</a></h4> 0337 * 0338 * <p>MessageFormats are not synchronized. 0339 * It is recommended to create separate format instances for each thread. 0340 * If multiple threads access a format concurrently, it must be synchronized 0341 * externally. 0342 * 0343 * @stable ICU 2.0 0344 */ 0345 class U_I18N_API MessageFormat : public Format { 0346 public: 0347 #ifndef U_HIDE_OBSOLETE_API 0348 /** 0349 * Enum type for kMaxFormat. 0350 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6, 0351 * rendering this enum type obsolete. 0352 */ 0353 enum EFormatNumber { 0354 /** 0355 * The maximum number of arguments. 0356 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6, 0357 * rendering this constant obsolete. 0358 */ 0359 kMaxFormat = 10 0360 }; 0361 #endif /* U_HIDE_OBSOLETE_API */ 0362 0363 /** 0364 * Constructs a new MessageFormat using the given pattern and the 0365 * default locale. 0366 * 0367 * @param pattern Pattern used to construct object. 0368 * @param status Input/output error code. If the 0369 * pattern cannot be parsed, set to failure code. 0370 * @stable ICU 2.0 0371 */ 0372 MessageFormat(const UnicodeString& pattern, 0373 UErrorCode &status); 0374 0375 /** 0376 * Constructs a new MessageFormat using the given pattern and locale. 0377 * @param pattern Pattern used to construct object. 0378 * @param newLocale The locale to use for formatting dates and numbers. 0379 * @param status Input/output error code. If the 0380 * pattern cannot be parsed, set to failure code. 0381 * @stable ICU 2.0 0382 */ 0383 MessageFormat(const UnicodeString& pattern, 0384 const Locale& newLocale, 0385 UErrorCode& status); 0386 /** 0387 * Constructs a new MessageFormat using the given pattern and locale. 0388 * @param pattern Pattern used to construct object. 0389 * @param newLocale The locale to use for formatting dates and numbers. 0390 * @param parseError Struct to receive information on the position 0391 * of an error within the pattern. 0392 * @param status Input/output error code. If the 0393 * pattern cannot be parsed, set to failure code. 0394 * @stable ICU 2.0 0395 */ 0396 MessageFormat(const UnicodeString& pattern, 0397 const Locale& newLocale, 0398 UParseError& parseError, 0399 UErrorCode& status); 0400 /** 0401 * Constructs a new MessageFormat from an existing one. 0402 * @stable ICU 2.0 0403 */ 0404 MessageFormat(const MessageFormat&); 0405 0406 /** 0407 * Assignment operator. 0408 * @stable ICU 2.0 0409 */ 0410 const MessageFormat& operator=(const MessageFormat&); 0411 0412 /** 0413 * Destructor. 0414 * @stable ICU 2.0 0415 */ 0416 virtual ~MessageFormat(); 0417 0418 /** 0419 * Clones this Format object polymorphically. The caller owns the 0420 * result and should delete it when done. 0421 * @stable ICU 2.0 0422 */ 0423 virtual MessageFormat* clone() const override; 0424 0425 /** 0426 * Returns true if the given Format objects are semantically equal. 0427 * Objects of different subclasses are considered unequal. 0428 * @param other the object to be compared with. 0429 * @return true if the given Format objects are semantically equal. 0430 * @stable ICU 2.0 0431 */ 0432 virtual bool operator==(const Format& other) const override; 0433 0434 /** 0435 * Sets the locale to be used for creating argument Format objects. 0436 * @param theLocale the new locale value to be set. 0437 * @stable ICU 2.0 0438 */ 0439 virtual void setLocale(const Locale& theLocale); 0440 0441 /** 0442 * Gets the locale used for creating argument Format objects. 0443 * format information. 0444 * @return the locale of the object. 0445 * @stable ICU 2.0 0446 */ 0447 virtual const Locale& getLocale(void) const; 0448 0449 /** 0450 * Applies the given pattern string to this message format. 0451 * 0452 * @param pattern The pattern to be applied. 0453 * @param status Input/output error code. If the 0454 * pattern cannot be parsed, set to failure code. 0455 * @stable ICU 2.0 0456 */ 0457 virtual void applyPattern(const UnicodeString& pattern, 0458 UErrorCode& status); 0459 /** 0460 * Applies the given pattern string to this message format. 0461 * 0462 * @param pattern The pattern to be applied. 0463 * @param parseError Struct to receive information on the position 0464 * of an error within the pattern. 0465 * @param status Input/output error code. If the 0466 * pattern cannot be parsed, set to failure code. 0467 * @stable ICU 2.0 0468 */ 0469 virtual void applyPattern(const UnicodeString& pattern, 0470 UParseError& parseError, 0471 UErrorCode& status); 0472 0473 /** 0474 * Sets the UMessagePatternApostropheMode and the pattern used by this message format. 0475 * Parses the pattern and caches Format objects for simple argument types. 0476 * Patterns and their interpretation are specified in the 0477 * <a href="#patterns">class description</a>. 0478 * <p> 0479 * This method is best used only once on a given object to avoid confusion about the mode, 0480 * and after constructing the object with an empty pattern string to minimize overhead. 0481 * 0482 * @param pattern The pattern to be applied. 0483 * @param aposMode The new apostrophe mode. 0484 * @param parseError Struct to receive information on the position 0485 * of an error within the pattern. 0486 * Can be nullptr. 0487 * @param status Input/output error code. If the 0488 * pattern cannot be parsed, set to failure code. 0489 * @stable ICU 4.8 0490 */ 0491 virtual void applyPattern(const UnicodeString& pattern, 0492 UMessagePatternApostropheMode aposMode, 0493 UParseError* parseError, 0494 UErrorCode& status); 0495 0496 /** 0497 * @return this instance's UMessagePatternApostropheMode. 0498 * @stable ICU 4.8 0499 */ 0500 UMessagePatternApostropheMode getApostropheMode() const { 0501 return msgPattern.getApostropheMode(); 0502 } 0503 0504 /** 0505 * Returns a pattern that can be used to recreate this object. 0506 * 0507 * @param appendTo Output parameter to receive the pattern. 0508 * Result is appended to existing contents. 0509 * @return Reference to 'appendTo' parameter. 0510 * @stable ICU 2.0 0511 */ 0512 virtual UnicodeString& toPattern(UnicodeString& appendTo) const; 0513 0514 /** 0515 * Sets subformats. 0516 * See the class description about format numbering. 0517 * The caller should not delete the Format objects after this call. 0518 * <EM>The array formatsToAdopt is not itself adopted.</EM> Its 0519 * ownership is retained by the caller. If the call fails because 0520 * memory cannot be allocated, then the formats will be deleted 0521 * by this method, and this object will remain unchanged. 0522 * 0523 * <p>If this format uses named arguments, the new formats are discarded 0524 * and this format remains unchanged. 0525 * 0526 * @stable ICU 2.0 0527 * @param formatsToAdopt the format to be adopted. 0528 * @param count the size of the array. 0529 */ 0530 virtual void adoptFormats(Format** formatsToAdopt, int32_t count); 0531 0532 /** 0533 * Sets subformats. 0534 * See the class description about format numbering. 0535 * Each item in the array is cloned into the internal array. 0536 * If the call fails because memory cannot be allocated, then this 0537 * object will remain unchanged. 0538 * 0539 * <p>If this format uses named arguments, the new formats are discarded 0540 * and this format remains unchanged. 0541 * 0542 * @stable ICU 2.0 0543 * @param newFormats the new format to be set. 0544 * @param cnt the size of the array. 0545 */ 0546 virtual void setFormats(const Format** newFormats, int32_t cnt); 0547 0548 0549 /** 0550 * Sets one subformat. 0551 * See the class description about format numbering. 0552 * The caller should not delete the Format object after this call. 0553 * If the number is over the number of formats already set, 0554 * the item will be deleted and ignored. 0555 * 0556 * <p>If this format uses named arguments, the new format is discarded 0557 * and this format remains unchanged. 0558 * 0559 * @stable ICU 2.0 0560 * @param formatNumber index of the subformat. 0561 * @param formatToAdopt the format to be adopted. 0562 */ 0563 virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt); 0564 0565 /** 0566 * Sets one subformat. 0567 * See the class description about format numbering. 0568 * If the number is over the number of formats already set, 0569 * the item will be ignored. 0570 * @param formatNumber index of the subformat. 0571 * @param format the format to be set. 0572 * @stable ICU 2.0 0573 */ 0574 virtual void setFormat(int32_t formatNumber, const Format& format); 0575 0576 /** 0577 * Gets format names. This function returns formatNames in StringEnumerations 0578 * which can be used with getFormat() and setFormat() to export formattable 0579 * array from current MessageFormat to another. It is the caller's responsibility 0580 * to delete the returned formatNames. 0581 * @param status output param set to success/failure code. 0582 * @stable ICU 4.0 0583 */ 0584 virtual StringEnumeration* getFormatNames(UErrorCode& status); 0585 0586 /** 0587 * Gets subformat pointer for given format name. 0588 * This function supports both named and numbered 0589 * arguments. If numbered, the formatName is the 0590 * corresponding UnicodeStrings (e.g. "0", "1", "2"...). 0591 * The returned Format object should not be deleted by the caller, 0592 * nor should the pointer of other object . The pointer and its 0593 * contents remain valid only until the next call to any method 0594 * of this class is made with this object. 0595 * @param formatName the name or number specifying a format 0596 * @param status output param set to success/failure code. 0597 * @stable ICU 4.0 0598 */ 0599 virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status); 0600 0601 /** 0602 * Sets one subformat for given format name. 0603 * See the class description about format name. 0604 * This function supports both named and numbered 0605 * arguments-- if numbered, the formatName is the 0606 * corresponding UnicodeStrings (e.g. "0", "1", "2"...). 0607 * If there is no matched formatName or wrong type, 0608 * the item will be ignored. 0609 * @param formatName Name of the subformat. 0610 * @param format the format to be set. 0611 * @param status output param set to success/failure code. 0612 * @stable ICU 4.0 0613 */ 0614 virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status); 0615 0616 /** 0617 * Sets one subformat for given format name. 0618 * See the class description about format name. 0619 * This function supports both named and numbered 0620 * arguments-- if numbered, the formatName is the 0621 * corresponding UnicodeStrings (e.g. "0", "1", "2"...). 0622 * If there is no matched formatName or wrong type, 0623 * the item will be ignored. 0624 * The caller should not delete the Format object after this call. 0625 * @param formatName Name of the subformat. 0626 * @param formatToAdopt Format to be adopted. 0627 * @param status output param set to success/failure code. 0628 * @stable ICU 4.0 0629 */ 0630 virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status); 0631 0632 /** 0633 * Gets an array of subformats of this object. The returned array 0634 * should not be deleted by the caller, nor should the pointers 0635 * within the array. The array and its contents remain valid only 0636 * until the next call to this format. See the class description 0637 * about format numbering. 0638 * 0639 * @param count output parameter to receive the size of the array 0640 * @return an array of count Format* objects, or nullptr if out of 0641 * memory. Any or all of the array elements may be nullptr. 0642 * @stable ICU 2.0 0643 */ 0644 virtual const Format** getFormats(int32_t& count) const; 0645 0646 0647 using Format::format; 0648 0649 /** 0650 * Formats the given array of arguments into a user-readable string. 0651 * Does not take ownership of the Formattable* array or its contents. 0652 * 0653 * <p>If this format uses named arguments, appendTo is unchanged and 0654 * status is set to U_ILLEGAL_ARGUMENT_ERROR. 0655 * 0656 * @param source An array of objects to be formatted. 0657 * @param count The number of elements of 'source'. 0658 * @param appendTo Output parameter to receive result. 0659 * Result is appended to existing contents. 0660 * @param ignore Not used; inherited from base class API. 0661 * @param status Input/output error code. If the 0662 * pattern cannot be parsed, set to failure code. 0663 * @return Reference to 'appendTo' parameter. 0664 * @stable ICU 2.0 0665 */ 0666 UnicodeString& format(const Formattable* source, 0667 int32_t count, 0668 UnicodeString& appendTo, 0669 FieldPosition& ignore, 0670 UErrorCode& status) const; 0671 0672 /** 0673 * Formats the given array of arguments into a user-readable string 0674 * using the given pattern. 0675 * 0676 * <p>If this format uses named arguments, appendTo is unchanged and 0677 * status is set to U_ILLEGAL_ARGUMENT_ERROR. 0678 * 0679 * @param pattern The pattern. 0680 * @param arguments An array of objects to be formatted. 0681 * @param count The number of elements of 'source'. 0682 * @param appendTo Output parameter to receive result. 0683 * Result is appended to existing contents. 0684 * @param status Input/output error code. If the 0685 * pattern cannot be parsed, set to failure code. 0686 * @return Reference to 'appendTo' parameter. 0687 * @stable ICU 2.0 0688 */ 0689 static UnicodeString& format(const UnicodeString& pattern, 0690 const Formattable* arguments, 0691 int32_t count, 0692 UnicodeString& appendTo, 0693 UErrorCode& status); 0694 0695 /** 0696 * Formats the given array of arguments into a user-readable 0697 * string. The array must be stored within a single Formattable 0698 * object of type kArray. If the Formattable object type is not of 0699 * type kArray, then returns a failing UErrorCode. 0700 * 0701 * <p>If this format uses named arguments, appendTo is unchanged and 0702 * status is set to U_ILLEGAL_ARGUMENT_ERROR. 0703 * 0704 * @param obj A Formattable of type kArray containing 0705 * arguments to be formatted. 0706 * @param appendTo Output parameter to receive result. 0707 * Result is appended to existing contents. 0708 * @param pos On input: an alignment field, if desired. 0709 * On output: the offsets of the alignment field. 0710 * @param status Input/output error code. If the 0711 * pattern cannot be parsed, set to failure code. 0712 * @return Reference to 'appendTo' parameter. 0713 * @stable ICU 2.0 0714 */ 0715 virtual UnicodeString& format(const Formattable& obj, 0716 UnicodeString& appendTo, 0717 FieldPosition& pos, 0718 UErrorCode& status) const override; 0719 0720 /** 0721 * Formats the given array of arguments into a user-defined argument name 0722 * array. This function supports both named and numbered 0723 * arguments-- if numbered, the formatName is the 0724 * corresponding UnicodeStrings (e.g. "0", "1", "2"...). 0725 * 0726 * @param argumentNames argument name array 0727 * @param arguments An array of objects to be formatted. 0728 * @param count The number of elements of 'argumentNames' and 0729 * arguments. The number of argumentNames and arguments 0730 * must be the same. 0731 * @param appendTo Output parameter to receive result. 0732 * Result is appended to existing contents. 0733 * @param status Input/output error code. If the 0734 * pattern cannot be parsed, set to failure code. 0735 * @return Reference to 'appendTo' parameter. 0736 * @stable ICU 4.0 0737 */ 0738 UnicodeString& format(const UnicodeString* argumentNames, 0739 const Formattable* arguments, 0740 int32_t count, 0741 UnicodeString& appendTo, 0742 UErrorCode& status) const; 0743 /** 0744 * Parses the given string into an array of output arguments. 0745 * 0746 * @param source String to be parsed. 0747 * @param pos On input, starting position for parse. On output, 0748 * final position after parse. Unchanged if parse 0749 * fails. 0750 * @param count Output parameter to receive the number of arguments 0751 * parsed. 0752 * @return an array of parsed arguments. The caller owns both 0753 * the array and its contents. 0754 * @stable ICU 2.0 0755 */ 0756 virtual Formattable* parse(const UnicodeString& source, 0757 ParsePosition& pos, 0758 int32_t& count) const; 0759 0760 /** 0761 * Parses the given string into an array of output arguments. 0762 * 0763 * <p>If this format uses named arguments, status is set to 0764 * U_ARGUMENT_TYPE_MISMATCH. 0765 * 0766 * @param source String to be parsed. 0767 * @param count Output param to receive size of returned array. 0768 * @param status Input/output error code. If the 0769 * pattern cannot be parsed, set to failure code. 0770 * @return an array of parsed arguments. The caller owns both 0771 * the array and its contents. Returns nullptr if status is not U_ZERO_ERROR. 0772 * 0773 * @stable ICU 2.0 0774 */ 0775 virtual Formattable* parse(const UnicodeString& source, 0776 int32_t& count, 0777 UErrorCode& status) const; 0778 0779 /** 0780 * Parses the given string into an array of output arguments 0781 * stored within a single Formattable of type kArray. 0782 * 0783 * @param source The string to be parsed into an object. 0784 * @param result Formattable to be set to the parse result. 0785 * If parse fails, return contents are undefined. 0786 * @param pos On input, starting position for parse. On output, 0787 * final position after parse. Unchanged if parse 0788 * fails. 0789 * @stable ICU 2.0 0790 */ 0791 virtual void parseObject(const UnicodeString& source, 0792 Formattable& result, 0793 ParsePosition& pos) const override; 0794 0795 /** 0796 * Convert an 'apostrophe-friendly' pattern into a standard 0797 * pattern. Standard patterns treat all apostrophes as 0798 * quotes, which is problematic in some languages, e.g. 0799 * French, where apostrophe is commonly used. This utility 0800 * assumes that only an unpaired apostrophe immediately before 0801 * a brace is a true quote. Other unpaired apostrophes are paired, 0802 * and the resulting standard pattern string is returned. 0803 * 0804 * <p><b>Note</b> it is not guaranteed that the returned pattern 0805 * is indeed a valid pattern. The only effect is to convert 0806 * between patterns having different quoting semantics. 0807 * 0808 * @param pattern the 'apostrophe-friendly' patttern to convert 0809 * @param status Input/output error code. If the pattern 0810 * cannot be parsed, the failure code is set. 0811 * @return the standard equivalent of the original pattern 0812 * @stable ICU 3.4 0813 */ 0814 static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern, 0815 UErrorCode& status); 0816 0817 0818 /** 0819 * Returns true if this MessageFormat uses named arguments, 0820 * and false otherwise. See class description. 0821 * 0822 * @return true if named arguments are used. 0823 * @stable ICU 4.0 0824 */ 0825 UBool usesNamedArguments() const; 0826 0827 0828 #ifndef U_HIDE_INTERNAL_API 0829 /** 0830 * This API is for ICU internal use only. 0831 * Please do not use it. 0832 * 0833 * Returns argument types count in the parsed pattern. 0834 * Used to distinguish pattern "{0} d" and "d". 0835 * 0836 * @return The number of formattable types in the pattern 0837 * @internal 0838 */ 0839 int32_t getArgTypeCount() const; 0840 #endif /* U_HIDE_INTERNAL_API */ 0841 0842 /** 0843 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 0844 * This method is to implement a simple version of RTTI, since not all 0845 * C++ compilers support genuine RTTI. Polymorphic operator==() and 0846 * clone() methods call this method. 0847 * 0848 * @return The class ID for this object. All objects of a 0849 * given class have the same class ID. Objects of 0850 * other classes have different class IDs. 0851 * @stable ICU 2.0 0852 */ 0853 virtual UClassID getDynamicClassID(void) const override; 0854 0855 /** 0856 * Return the class ID for this class. This is useful only for 0857 * comparing to a return value from getDynamicClassID(). For example: 0858 * <pre> 0859 * . Base* polymorphic_pointer = createPolymorphicObject(); 0860 * . if (polymorphic_pointer->getDynamicClassID() == 0861 * . Derived::getStaticClassID()) ... 0862 * </pre> 0863 * @return The class ID for all objects of this class. 0864 * @stable ICU 2.0 0865 */ 0866 static UClassID U_EXPORT2 getStaticClassID(void); 0867 0868 #ifndef U_HIDE_INTERNAL_API 0869 /** 0870 * Compares two Format objects. This is used for constructing the hash 0871 * tables. 0872 * 0873 * @param left pointer to a Format object. Must not be nullptr. 0874 * @param right pointer to a Format object. Must not be nullptr. 0875 * 0876 * @return whether the two objects are the same 0877 * @internal 0878 */ 0879 static UBool equalFormats(const void* left, const void* right); 0880 #endif /* U_HIDE_INTERNAL_API */ 0881 0882 private: 0883 0884 Locale fLocale; 0885 MessagePattern msgPattern; 0886 Format** formatAliases; // see getFormats 0887 int32_t formatAliasesCapacity; 0888 0889 MessageFormat() = delete; // default constructor not implemented 0890 0891 /** 0892 * This provider helps defer instantiation of a PluralRules object 0893 * until we actually need to select a keyword. 0894 * For example, if the number matches an explicit-value selector like "=1" 0895 * we do not need any PluralRules. 0896 */ 0897 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector { 0898 public: 0899 PluralSelectorProvider(const MessageFormat &mf, UPluralType type); 0900 virtual ~PluralSelectorProvider(); 0901 virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const override; 0902 0903 void reset(); 0904 private: 0905 const MessageFormat &msgFormat; 0906 PluralRules* rules; 0907 UPluralType type; 0908 }; 0909 0910 /** 0911 * A MessageFormat formats an array of arguments. Each argument 0912 * has an expected type, based on the pattern. For example, if 0913 * the pattern contains the subformat "{3,number,integer}", then 0914 * we expect argument 3 to have type Formattable::kLong. This 0915 * array needs to grow dynamically if the MessageFormat is 0916 * modified. 0917 */ 0918 Formattable::Type* argTypes; 0919 int32_t argTypeCount; 0920 int32_t argTypeCapacity; 0921 0922 /** 0923 * true if there are different argTypes for the same argument. 0924 * This only matters when the MessageFormat is used in the plain C (umsg_xxx) API 0925 * where the pattern argTypes determine how the va_arg list is read. 0926 */ 0927 UBool hasArgTypeConflicts; 0928 0929 // Variable-size array management 0930 UBool allocateArgTypes(int32_t capacity, UErrorCode& status); 0931 0932 /** 0933 * Default Format objects used when no format is specified and a 0934 * numeric or date argument is formatted. These are volatile 0935 * cache objects maintained only for performance. They do not 0936 * participate in operator=(), copy constructor(), nor 0937 * operator==(). 0938 */ 0939 NumberFormat* defaultNumberFormat; 0940 DateFormat* defaultDateFormat; 0941 0942 UHashtable* cachedFormatters; 0943 UHashtable* customFormatArgStarts; 0944 0945 PluralSelectorProvider pluralProvider; 0946 PluralSelectorProvider ordinalProvider; 0947 0948 /** 0949 * Method to retrieve default formats (or nullptr on failure). 0950 * These are semantically const, but may modify *this. 0951 */ 0952 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const; 0953 const DateFormat* getDefaultDateFormat(UErrorCode&) const; 0954 0955 /** 0956 * Finds the word s, in the keyword list and returns the located index. 0957 * @param s the keyword to be searched for. 0958 * @param list the list of keywords to be searched with. 0959 * @return the index of the list which matches the keyword s. 0960 */ 0961 static int32_t findKeyword( const UnicodeString& s, 0962 const char16_t * const *list); 0963 0964 /** 0965 * Thin wrapper around the format(... AppendableWrapper ...) variant. 0966 * Wraps the destination UnicodeString into an AppendableWrapper and 0967 * supplies default values for some other parameters. 0968 */ 0969 UnicodeString& format(const Formattable* arguments, 0970 const UnicodeString *argumentNames, 0971 int32_t cnt, 0972 UnicodeString& appendTo, 0973 FieldPosition* pos, 0974 UErrorCode& status) const; 0975 0976 /** 0977 * Formats the arguments and writes the result into the 0978 * AppendableWrapper, updates the field position. 0979 * 0980 * @param msgStart Index to msgPattern part to start formatting from. 0981 * @param plNumber nullptr except when formatting a plural argument sub-message 0982 * where a '#' is replaced by the format string for this number. 0983 * @param arguments The formattable objects array. (Must not be nullptr.) 0984 * @param argumentNames nullptr if numbered values are used. Otherwise the same 0985 * length as "arguments", and each entry is the name of the 0986 * corresponding argument in "arguments". 0987 * @param cnt The length of arguments (and of argumentNames if that is not nullptr). 0988 * @param appendTo Output parameter to receive the result. 0989 * The result string is appended to existing contents. 0990 * @param pos Field position status. 0991 * @param success The error code status. 0992 */ 0993 void format(int32_t msgStart, 0994 const void *plNumber, 0995 const Formattable* arguments, 0996 const UnicodeString *argumentNames, 0997 int32_t cnt, 0998 AppendableWrapper& appendTo, 0999 FieldPosition* pos, 1000 UErrorCode& success) const; 1001 1002 UnicodeString getArgName(int32_t partIndex); 1003 1004 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status); 1005 1006 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status); 1007 1008 int32_t nextTopLevelArgStart(int32_t partIndex) const; 1009 1010 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber); 1011 1012 void cacheExplicitFormats(UErrorCode& status); 1013 1014 Format* createAppropriateFormat(UnicodeString& type, 1015 UnicodeString& style, 1016 Formattable::Type& formattableType, 1017 UParseError& parseError, 1018 UErrorCode& ec); 1019 1020 const Formattable* getArgFromListByName(const Formattable* arguments, 1021 const UnicodeString *argumentNames, 1022 int32_t cnt, UnicodeString& name) const; 1023 1024 Formattable* parse(int32_t msgStart, 1025 const UnicodeString& source, 1026 ParsePosition& pos, 1027 int32_t& count, 1028 UErrorCode& ec) const; 1029 1030 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength, 1031 FieldPosition* fp, const Formattable* argId) const; 1032 1033 /** 1034 * Finds the "other" sub-message. 1035 * @param partIndex the index of the first PluralFormat argument style part. 1036 * @return the "other" sub-message start part index. 1037 */ 1038 int32_t findOtherSubMessage(int32_t partIndex) const; 1039 1040 /** 1041 * Returns the ARG_START index of the first occurrence of the plural number in a sub-message. 1042 * Returns -1 if it is a REPLACE_NUMBER. 1043 * Returns 0 if there is neither. 1044 */ 1045 int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const; 1046 1047 Format* getCachedFormatter(int32_t argumentNumber) const; 1048 1049 UnicodeString getLiteralStringUntilNextArgument(int32_t from) const; 1050 1051 void copyObjects(const MessageFormat& that, UErrorCode& ec); 1052 1053 void formatComplexSubMessage(int32_t msgStart, 1054 const void *plNumber, 1055 const Formattable* arguments, 1056 const UnicodeString *argumentNames, 1057 int32_t cnt, 1058 AppendableWrapper& appendTo, 1059 UErrorCode& success) const; 1060 1061 /** 1062 * Convenience method that ought to be in NumberFormat 1063 */ 1064 NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const; 1065 1066 /** 1067 * Returns array of argument types in the parsed pattern 1068 * for use in C API. Only for the use of umsg_vformat(). Not 1069 * for public consumption. 1070 * @param listCount Output parameter to receive the size of array 1071 * @return The array of formattable types in the pattern 1072 */ 1073 const Formattable::Type* getArgTypeList(int32_t& listCount) const { 1074 listCount = argTypeCount; 1075 return argTypes; 1076 } 1077 1078 /** 1079 * Resets the internal MessagePattern, and other associated caches. 1080 */ 1081 void resetPattern(); 1082 1083 /** 1084 * A DummyFormatter that we use solely to store a nullptr value. UHash does 1085 * not support storing nullptr values. 1086 */ 1087 class U_I18N_API DummyFormat : public Format { 1088 public: 1089 virtual bool operator==(const Format&) const override; 1090 virtual DummyFormat* clone() const override; 1091 virtual UnicodeString& format(const Formattable& obj, 1092 UnicodeString& appendTo, 1093 UErrorCode& status) const; 1094 virtual UnicodeString& format(const Formattable&, 1095 UnicodeString& appendTo, 1096 FieldPosition&, 1097 UErrorCode& status) const override; 1098 virtual UnicodeString& format(const Formattable& obj, 1099 UnicodeString& appendTo, 1100 FieldPositionIterator* posIter, 1101 UErrorCode& status) const override; 1102 virtual void parseObject(const UnicodeString&, 1103 Formattable&, 1104 ParsePosition&) const override; 1105 }; 1106 1107 friend class MessageFormatAdapter; // getFormatTypeList() access 1108 }; 1109 1110 U_NAMESPACE_END 1111 1112 #endif /* #if !UCONFIG_NO_FORMATTING */ 1113 1114 #endif /* U_SHOW_CPLUSPLUS_API */ 1115 1116 #endif // _MSGFMT 1117 //eof
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |