Public Member Functions |
| U_I18N_API | MessageFormat (constUnicodeString &pattern,UErrorCode &status) |
| | Constructs a newMessageFormat using the given pattern and the default locale.More...
|
| |
| U_I18N_API | MessageFormat (constUnicodeString &pattern, constLocale &newLocale,UErrorCode &status) |
| | Constructs a newMessageFormat using the given pattern and locale.More...
|
| |
| U_I18N_API | MessageFormat (constUnicodeString &pattern, constLocale &newLocale,UParseError &parseError,UErrorCode &status) |
| | Constructs a newMessageFormat using the given pattern and locale.More...
|
| |
| U_I18N_API | MessageFormat (constMessageFormat &) |
| | Constructs a newMessageFormat from an existing one.More...
|
| |
| U_I18N_API constMessageFormat & | operator= (constMessageFormat &) |
| | Assignment operator.More...
|
| |
| virtualU_I18N_API | ~MessageFormat () |
| | Destructor.More...
|
| |
| virtualU_I18N_APIMessageFormat * | clone () const override |
| | Clones thisFormat object polymorphically.More...
|
| |
| virtualU_I18N_API bool | operator== (constFormat &other) const override |
| | Returns true if the givenFormat objects are semantically equal.More...
|
| |
| virtualU_I18N_API void | setLocale (constLocale &theLocale) |
| | Sets the locale to be used for creating argumentFormat objects.More...
|
| |
| virtualU_I18N_API constLocale & | getLocale () const |
| | Gets the locale used for creating argumentFormat objects.More...
|
| |
| virtualU_I18N_API void | applyPattern (constUnicodeString &pattern,UErrorCode &status) |
| | Applies the given pattern string to this message format.More...
|
| |
| virtualU_I18N_API void | applyPattern (constUnicodeString &pattern,UParseError &parseError,UErrorCode &status) |
| | Applies the given pattern string to this message format.More...
|
| |
| virtualU_I18N_API void | applyPattern (constUnicodeString &pattern,UMessagePatternApostropheMode aposMode,UParseError *parseError,UErrorCode &status) |
| | Sets the UMessagePatternApostropheMode and the pattern used by this message format.More...
|
| |
| U_I18N_APIUMessagePatternApostropheMode | getApostropheMode () const |
| |
| virtualU_I18N_APIUnicodeString & | toPattern (UnicodeString &appendTo) const |
| | Returns a pattern that can be used to recreate this object.More...
|
| |
| virtualU_I18N_API void | adoptFormats (Format **formatsToAdopt, int32_t count) |
| | Sets subformats.More...
|
| |
| virtualU_I18N_API void | setFormats (constFormat **newFormats, int32_t cnt) |
| | Sets subformats.More...
|
| |
| virtualU_I18N_API void | adoptFormat (int32_t formatNumber,Format *formatToAdopt) |
| | Sets one subformat.More...
|
| |
| virtualU_I18N_API void | setFormat (int32_t formatNumber, constFormat &format) |
| | Sets one subformat.More...
|
| |
| virtualU_I18N_APIStringEnumeration * | getFormatNames (UErrorCode &status) |
| | Gets format names.More...
|
| |
| virtualU_I18N_APIFormat * | getFormat (constUnicodeString &formatName,UErrorCode &status) |
| | Gets subformat pointer for given format name.More...
|
| |
| virtualU_I18N_API void | setFormat (constUnicodeString &formatName, constFormat &format,UErrorCode &status) |
| | Sets one subformat for given format name.More...
|
| |
| virtualU_I18N_API void | adoptFormat (constUnicodeString &formatName,Format *formatToAdopt,UErrorCode &status) |
| | Sets one subformat for given format name.More...
|
| |
| virtualU_I18N_API constFormat ** | getFormats (int32_t &count) const |
| | Gets an array of subformats of this object.More...
|
| |
| U_I18N_APIUnicodeString & | format (constFormattable *source, int32_t count,UnicodeString &appendTo,FieldPosition &ignore,UErrorCode &status) const |
| | Formats the given array of arguments into a user-readable string.More...
|
| |
| virtualU_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const override |
| | Formats the given array of arguments into a user-readable string.More...
|
| |
| U_I18N_APIUnicodeString & | format (constUnicodeString *argumentNames, constFormattable *arguments, int32_t count,UnicodeString &appendTo,UErrorCode &status) const |
| | Formats the given array of arguments into a user-defined argument name array.More...
|
| |
| virtualU_I18N_APIFormattable * | parse (constUnicodeString &source,ParsePosition &pos, int32_t &count) const |
| | Parses the given string into an array of output arguments.More...
|
| |
| virtualU_I18N_APIFormattable * | parse (constUnicodeString &source, int32_t &count,UErrorCode &status) const |
| | Parses the given string into an array of output arguments.More...
|
| |
| virtualU_I18N_API void | parseObject (constUnicodeString &source,Formattable &result,ParsePosition &pos) const override |
| | Parses the given string into an array of output arguments stored within a singleFormattable of type kArray.More...
|
| |
| U_I18N_APIUBool | usesNamedArguments () const |
| | Returns true if thisMessageFormat uses named arguments, and false otherwise.More...
|
| |
| U_I18N_API int32_t | getArgTypeCount () const |
| | This API is for ICU internal use only.More...
|
| |
| virtualU_I18N_APIUClassID | getDynamicClassID () const override |
| | Returns a unique class ID POLYMORPHICALLY.More...
|
| |
| U_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const |
| | Formats an object to produce a string.More...
|
| |
| virtualU_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const=0 |
| | Format an object to produce a string.More...
|
| |
| virtualU_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const |
| | Format an object to produce a string.More...
|
| |
Public Member Functions inherited fromicu::Format |
| virtualU_I18N_API | ~Format () |
| | Destructor.More...
|
| |
| U_I18N_API bool | operator!= (constFormat &other) const |
| | Return true if the givenFormat objects are not semantically equal.More...
|
| |
| U_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const |
| | Formats an object to produce a string.More...
|
| |
| virtualU_I18N_APIUnicodeString & | format (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const |
| | Format an object to produce a string.More...
|
| |
| U_I18N_API void | parseObject (constUnicodeString &source,Formattable &result,UErrorCode &status) const |
| | Parses a string to produce an object.More...
|
| |
| U_I18N_APILocale | getLocale (ULocDataLocaleType type,UErrorCode &status) const |
| | Get the locale for this format object.More...
|
| |
| U_I18N_API const char * | getLocaleID (ULocDataLocaleType type,UErrorCode &status) const |
| | Get the locale for this format object.More...
|
| |
Public Member Functions inherited fromicu::UObject |
| virtual | ~UObject () |
| | Destructor.More...
|
| |
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.
AMessageFormat is constructed from apattern string with arguments in {curly braces} which will be replaced by formatted values.
MessageFormat differs from the otherFormat classes in that you create aMessageFormat object with one of its constructors (not with acreateInstance style factory method). Factory methods aren't necessary becauseMessageFormat 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.
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 (seeusesNamedArguments()).
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.
An argument might specify a "simple" type for which the specifiedFormat object is created, cached and used.
An argument might have a "complex" type with nestedMessageFormat sub-patterns. During formatting, one of these sub-messages is selected according to the argument value and recursively formatted.
After construction, a customFormat 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.
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).
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 customFormat 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).
Patterns and Their Interpretation
MessageFormat uses patterns of the following form:
message = messageText (argument messageText)*argument = noneArg | simpleArg | complexArgcomplexArg = choiceArg | pluralArg | selectArg | selectordinalArgnoneArg = '{' argNameOrNumber '}'simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'choiceStyle: seeChoiceFormatpluralStyle: seePluralFormatselectStyle: seeSelectFormatargNameOrNumber = argName | argNumberargName = [^[[: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- 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.
- 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.
- See alsoUMessagePatternApostropheMode
- In argStyleText, every single ASCII apostrophe begins and ends quoted literal text, and unquoted {curly braces} must occur in matched pairs.
Recommendation: Use the real apostrophe (single quote) character ’ (U+2019) for human-readable text, and use the ASCII apostrophe ' (U+0027) only in program syntax, like quoting inMessageFormat. See the annotations for U+0027 Apostrophe in The Unicode Standard.
Thechoice argument type is deprecated. Useplural arguments for proper plural selection, andselect arguments for simple selection among a fixed set of choices.
TheargType andargStyle values are used to create aFormat instance for the format element. The following table shows how the values map toFormat instances. Combinations not shown in the table are illegal. AnyargStyleText must be a valid pattern string for theFormat subclass used.
| argType | argStyle | resultingFormat object |
|---|
| (none) | null |
number | (none) | NumberFormat.createInstance(getLocale(), status) |
integer | NumberFormat.createInstance(getLocale(), kNumberStyle, status) |
currency | NumberFormat.createCurrencyInstance(getLocale(), status) |
percent | NumberFormat.createPercentInstance(getLocale(), status) |
| argStyleText | newDecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status) |
| argSkeletonText | NumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status) |
date | (none) | DateFormat.createDateInstance(kDefault, getLocale(), status) |
short | DateFormat.createDateInstance(kShort, getLocale(), status) |
medium | DateFormat.createDateInstance(kDefault, getLocale(), status) |
long | DateFormat.createDateInstance(kLong, getLocale(), status) |
full | DateFormat.createDateInstance(kFull, getLocale(), status) |
| argStyleText | new SimpleDateFormat(argStyleText, getLocale(), status) |
| argSkeletonText | DateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status) |
time | (none) | DateFormat.createTimeInstance(kDefault, getLocale(), status) |
short | DateFormat.createTimeInstance(kShort, getLocale(), status) |
medium | DateFormat.createTimeInstance(kDefault, getLocale(), status) |
long | DateFormat.createTimeInstance(kLong, getLocale(), status) |
full | DateFormat.createTimeInstance(kFull, getLocale(), status) |
| argStyleText | new SimpleDateFormat(argStyleText, getLocale(), status) |
spellout | argStyleText (optional) | new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status) .setDefaultRuleset(argStyleText, status); |
ordinal | argStyleText (optional) | new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status) .setDefaultRuleset(argStyleText, status); |
duration | argStyleText (optional) | new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status) .setDefaultRuleset(argStyleText, status); |
Argument formatting
Arguments are formatted according to their type, using the default ICU formatters for those types, unless otherwise specified.
There are also several ways to control the formatting.
We recommend you use default styles, predefined style values, skeletons, or preformatted values, but not pattern strings or custom format objects.
For more details, see theICU User Guide.
Usage Information
Here are some examples of usage: Example 1:
GregorianCalendar cal(success);
Formattable arguments[] = {
7L,
Formattable( (Date) cal.getTime(success), Formattable::kIsDate),
"a disturbance in the Force"
};
UnicodeString result;
"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.
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
@ U_ZERO_ERROR
No error, no warning.
Typically, the message format will come from resources, and the arguments will be dynamically set at runtime.
Example 2:
Formattable testArgs[] = {3L,"MyDisk"};
"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).
U_I18N_API MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.
For messages that include plural forms, you can use a plural argument:
"{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);
output:There are no files on disk "MyDisk".There are 3 files on "MyDisk".
SeePluralFormat andPluralRules for details.
Synchronization
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
Definition at line345 of filemsgfmt.h.