// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* * Copyright (C) 2007-2013, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************** * * File MSGFMT.H * * Modification History: * * Date Name Description * 02/19/97 aliu Converted from java. * 03/20/97 helena Finished first cut of implementation. * 07/22/98 stephen Removed operator!= (defined in Format) * 08/19/2002 srl Removing Javaisms *******************************************************************************/ #ifndef MSGFMT_H #define MSGFMT_H #include "unicode/utypes.h" #if U_SHOW_CPLUSPLUS_API /** * \file * \brief C++ API: Formats messages in a language-neutral way. */ #if !UCONFIG_NO_FORMATTING #include "unicode/format.h" #include "unicode/locid.h" #include "unicode/messagepattern.h" #include "unicode/parseerr.h" #include "unicode/plurfmt.h" #include "unicode/plurrule.h" U_CDECL_BEGIN // Forward declaration. struct UHashtable; UHashtable; /**< @internal */ U_CDECL_END U_NAMESPACE_BEGIN class AppendableWrapper; class DateFormat; class NumberFormat; /** * <p>MessageFormat prepares strings for display to users, * with optional arguments (variables/placeholders). * The arguments can occur in any order, which is necessary for translation * into languages with different grammars. * * <p>A MessageFormat is constructed from a <em>pattern</em> string * with arguments in {curly braces} which will be replaced by formatted values. * * <p><code>MessageFormat</code> differs from the other <code>Format</code> * classes in that you create a <code>MessageFormat</code> object with one * of its constructors (not with a <code>createInstance</code> style factory * method). Factory methods aren't necessary because <code>MessageFormat</code> * itself doesn't implement locale-specific behavior. Any locale-specific * behavior is defined by the pattern that you provide and the * subformats used for inserted arguments. * * <p>Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers). * Some of the API methods work only with argument numbers and throw an exception * if the pattern has named arguments (see {@link #usesNamedArguments()}). * * <p>An argument might not specify any format type. In this case, * a numeric value is formatted with a default (for the locale) NumberFormat, * and a date/time value is formatted with a default (for the locale) DateFormat. * * <p>An argument might specify a "simple" type for which the specified * Format object is created, cached and used. * * <p>An argument might have a "complex" type with nested MessageFormat sub-patterns. * During formatting, one of these sub-messages is selected according to the argument value * and recursively formatted. * * <p>After construction, a custom Format object can be set for * a top-level argument, overriding the default formatting and parsing behavior * for that argument. * However, custom formatting can be achieved more simply by writing * a typeless argument in the pattern string * and supplying it with a preformatted string value. * * <p>When formatting, MessageFormat takes a collection of argument values * and writes an output string. * The argument values may be passed as an array * (when the pattern contains only numbered arguments) * or as an array of names and and an array of arguments (which works for both named * and numbered arguments). * * <p>Each argument is matched with one of the input values by array index or argument name * and formatted according to its pattern specification * (or using a custom Format object if one was set). * A numbered pattern argument is matched with an argument name that contains that number * as an ASCII-decimal-digit string (without leading zero). * * <h4><a name="patterns">Patterns and Their Interpretation</a></h4> * * <code>MessageFormat</code> uses patterns of the following form: * <pre> * message = messageText (argument messageText)* * argument = noneArg | simpleArg | complexArg * complexArg = choiceArg | pluralArg | selectArg | selectordinalArg * * noneArg = '{' argNameOrNumber '}' * simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}' * choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}' * pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}' * selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}' * selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}' * * choiceStyle: see {@link ChoiceFormat} * pluralStyle: see {@link PluralFormat} * selectStyle: see {@link SelectFormat} * * argNameOrNumber = argName | argNumber * argName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ * argNumber = '0' | ('1'..'9' ('0'..'9')*) * * argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration" * argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText | "::" argSkeletonText * </pre> * * <ul> * <li>messageText can contain quoted literal strings including syntax characters. * A quoted literal string begins with an ASCII apostrophe and a syntax character * (usually a {curly brace}) and continues until the next single apostrophe. * A double ASCII apostrophe inside or outside of a quoted string represents * one literal apostrophe. * <li>Quotable syntax characters are the {curly braces} in all messageText parts, * plus the '#' sign in a messageText immediately inside a pluralStyle, * and the '|' symbol in a messageText immediately inside a choiceStyle. * <li>See also {@link #UMessagePatternApostropheMode} * <li>In argStyleText, every single ASCII apostrophe begins and ends quoted literal text, * and unquoted {curly braces} must occur in matched pairs. * </ul> * * <p>Recommendation: Use the real apostrophe (single quote) character * \htmlonly’\endhtmlonly (U+2019) for * human-readable text, and use the ASCII apostrophe ' (U+0027) * only in program syntax, like quoting in MessageFormat. * See the annotations for U+0027 Apostrophe in The Unicode Standard. * * <p>The <code>choice</code> argument type is deprecated. * Use <code>plural</code> arguments for proper plural selection, * and <code>select</code> arguments for simple selection among a fixed set of choices. * * <p>The <code>argType</code> and <code>argStyle</code> values are used to create * a <code>Format</code> instance for the format element. The following * table shows how the values map to Format instances. Combinations not * shown in the table are illegal. Any <code>argStyleText</code> must * be a valid pattern string for the Format subclass used. * * <p><table border=1> * <tr> * <th>argType * <th>argStyle * <th>resulting Format object * <tr> * <td colspan=2><i>(none)</i> * <td><code>null</code> * <tr> * <td rowspan=6><code>number</code> * <td><i>(none)</i> * <td><code>NumberFormat.createInstance(getLocale(), status)</code> * <tr> * <td><code>integer</code> * <td><code>NumberFormat.createInstance(getLocale(), kNumberStyle, status)</code> * <tr> * <td><code>currency</code> * <td><code>NumberFormat.createCurrencyInstance(getLocale(), status)</code> * <tr> * <td><code>percent</code> * <td><code>NumberFormat.createPercentInstance(getLocale(), status)</code> * <tr> * <td><i>argStyleText</i> * <td><code>new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status)</code> * <tr> * <td><i>argSkeletonText</i> * <td><code>NumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status)</code> * <tr> * <td rowspan=7><code>date</code> * <td><i>(none)</i> * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code> * <tr> * <td><code>short</code> * <td><code>DateFormat.createDateInstance(kShort, getLocale(), status)</code> * <tr> * <td><code>medium</code> * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code> * <tr> * <td><code>long</code> * <td><code>DateFormat.createDateInstance(kLong, getLocale(), status)</code> * <tr> * <td><code>full</code> * <td><code>DateFormat.createDateInstance(kFull, getLocale(), status)</code> * <tr> * <td><i>argStyleText</i> * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code> * <tr> * <td><i>argSkeletonText</i> * <td><code>DateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status)</code> * <tr> * <td rowspan=6><code>time</code> * <td><i>(none)</i> * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code> * <tr> * <td><code>short</code> * <td><code>DateFormat.createTimeInstance(kShort, getLocale(), status)</code> * <tr> * <td><code>medium</code> * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code> * <tr> * <td><code>long</code> * <td><code>DateFormat.createTimeInstance(kLong, getLocale(), status)</code> * <tr> * <td><code>full</code> * <td><code>DateFormat.createTimeInstance(kFull, getLocale(), status)</code> * <tr> * <td><i>argStyleText</i> * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code> * <tr> * <td><code>spellout</code> * <td><i>argStyleText (optional)</i> * <td><code>new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status) * <br/> .setDefaultRuleset(argStyleText, status);</code> * <tr> * <td><code>ordinal</code> * <td><i>argStyleText (optional)</i> * <td><code>new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status) * <br/> .setDefaultRuleset(argStyleText, status);</code> * <tr> * <td><code>duration</code> * <td><i>argStyleText (optional)</i> * <td><code>new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status) * <br/> .setDefaultRuleset(argStyleText, status);</code> * </table> * <p> * * <h4>Argument formatting</h4> * * <p>Arguments are formatted according to their type, using the default * ICU formatters for those types, unless otherwise specified.</p> * * <p>There are also several ways to control the formatting.</p> * * <p>We recommend you use default styles, predefined style values, skeletons, * or preformatted values, but not pattern strings or custom format objects.</p> * * <p>For more details, see the * <a href="https://unicode-org.github.io/icu/userguide/format_parse/messages">ICU User Guide</a>.</p> * * <h4>Usage Information</h4> * * <p>Here are some examples of usage: * Example 1: * * <pre> * \code * UErrorCode success = U_ZERO_ERROR; * GregorianCalendar cal(success); * Formattable arguments[] = { * 7L, * Formattable( (Date) cal.getTime(success), Formattable::kIsDate), * "a disturbance in the Force" * }; * * UnicodeString result; * MessageFormat::format( * "At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {0,number}.", * arguments, 3, result, success ); * * cout << "result: " << result << endl; * //<output>: At 4:34 PM on March 23, there was a disturbance * // in the Force on planet 7. * \endcode * </pre> * * Typically, the message format will come from resources, and the * arguments will be dynamically set at runtime. * * <p>Example 2: * * <pre> * \code * success = U_ZERO_ERROR; * Formattable testArgs[] = {3L, "MyDisk"}; * * MessageFormat form( * "The disk \"{1}\" contains {0} file(s).", success ); * * UnicodeString string; * FieldPosition fpos = 0; * cout << "format: " << form.format(testArgs, 2, string, fpos, success ) << endl; * * // output, with different testArgs: * // output: The disk "MyDisk" contains 0 file(s). * // output: The disk "MyDisk" contains 1 file(s). * // output: The disk "MyDisk" contains 1,273 file(s). * \endcode * </pre> * * * <p>For messages that include plural forms, you can use a plural argument: * <pre> * \code * success = U_ZERO_ERROR; * MessageFormat msgFmt( * "{num_files, plural, " * "=0{There are no files on disk \"{disk_name}\".}" * "=1{There is one file on disk \"{disk_name}\".}" * "other{There are # files on disk \"{disk_name}\".}}", * Locale("en"), * success); * FieldPosition fpos = 0; * Formattable testArgs[] = {0L, "MyDisk"}; * UnicodeString testArgsNames[] = {"num_files", "disk_name"}; * UnicodeString result; * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success); * testArgs[0] = 3L; * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success); * \endcode * <em>output</em>: * There are no files on disk "MyDisk". * There are 3 files on "MyDisk". * </pre> * See {@link PluralFormat} and {@link PluralRules} for details. * * <h4><a name="synchronization">Synchronization</a></h4> * * <p>MessageFormats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * externally. * * @stable ICU 2.0 */ class U_I18N_API MessageFormat : public Format { … }; U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ #endif /* U_SHOW_CPLUSPLUS_API */ #endif // _MSGFMT //eof