Logo Search packages:      
Sourcecode: icu version File versions  Download package

MessageFormat Class Reference

#include <msgfmt.h>

Inheritance diagram for MessageFormat:
Collaboration diagram for MessageFormat:

List of all members.


class  DummyFormat
class  PluralSelectorProvider

Public Types

enum  EFormatNumber { kMaxFormat = 10 }

Public Member Functions

virtual void adoptFormat (int32_t formatNumber, Format *formatToAdopt)
virtual void adoptFormat (const UnicodeString &formatName, Format *formatToAdopt, UErrorCode &status)
virtual void adoptFormats (Format **formatsToAdopt, int32_t count)
virtual void applyPattern (const UnicodeString &pattern, UErrorCode &status)
virtual void applyPattern (const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
virtual void applyPattern (const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
virtual Formatclone (void) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
UnicodeStringformat (const Formattable *source, int32_t count, UnicodeString &appendTo, FieldPosition &ignore, UErrorCode &status) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
UnicodeStringformat (const UnicodeString *argumentNames, const Formattable *arguments, int32_t count, UnicodeString &appendTo, UErrorCode &status) const
UMessagePatternApostropheMode getApostropheMode () const
int32_t getArgTypeCount () const
virtual UClassID getDynamicClassID (void) const
virtual FormatgetFormat (const UnicodeString &formatName, UErrorCode &status)
virtual StringEnumerationgetFormatNames (UErrorCode &status)
virtual const Format ** getFormats (int32_t &count) const
Locale getLocale (ULocDataLocaleType type, UErrorCode &status) const
virtual const LocalegetLocale (void) const
const char * getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
 MessageFormat (const UnicodeString &pattern, UErrorCode &status)
 MessageFormat (const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
 MessageFormat (const UnicodeString &pattern, const Locale &newLocale, UParseError &parseError, UErrorCode &status)
 MessageFormat (const MessageFormat &)
UBool operator!= (const Format &other) const
const MessageFormatoperator= (const MessageFormat &)
virtual UBool operator== (const Format &other) const
virtual Formattableparse (const UnicodeString &source, ParsePosition &pos, int32_t &count) const
virtual Formattableparse (const UnicodeString &source, int32_t &count, UErrorCode &status) const
void parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
virtual void parseObject (const UnicodeString &source, Formattable &result, ParsePosition &pos) const
virtual void setFormat (int32_t formatNumber, const Format &format)
virtual void setFormat (const UnicodeString &formatName, const Format &format, UErrorCode &status)
virtual void setFormats (const Format **newFormats, int32_t cnt)
virtual void setLocale (const Locale &theLocale)
virtual UnicodeStringtoPattern (UnicodeString &appendTo) const
UBool usesNamedArguments () const
virtual ~MessageFormat ()

Static Public Member Functions

static UnicodeString autoQuoteApostrophe (const UnicodeString &pattern, UErrorCode &status)
static UBool equalFormats (const void *left, const void *right)
static UnicodeStringformat (const UnicodeString &pattern, const Formattable *arguments, int32_t count, UnicodeString &appendTo, UErrorCode &status)
static UClassID U_EXPORT2 getStaticClassID (void)
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Protected Member Functions

void setLocaleIDs (const char *valid, const char *actual)

Static Protected Member Functions

static void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Private Member Functions

UBool allocateArgTypes (int32_t capacity, UErrorCode &status)
UBool argNameMatches (int32_t partIndex, const UnicodeString &argName, int32_t argNumber)
void cacheExplicitFormats (UErrorCode &status)
void copyObjects (const MessageFormat &that, UErrorCode &ec)
FormatcreateAppropriateFormat (UnicodeString &type, UnicodeString &style, Formattable::Type &formattableType, UParseError &parseError, UErrorCode &ec)
NumberFormatcreateIntegerFormat (const Locale &locale, UErrorCode &status) const
UnicodeStringformat (const Formattable *arguments, const UnicodeString *argumentNames, int32_t cnt, UnicodeString &appendTo, FieldPosition *pos, UErrorCode &status) const
void format (int32_t msgStart, double pluralNumber, const Formattable *arguments, const UnicodeString *argumentNames, int32_t cnt, AppendableWrapper &appendTo, FieldPosition *pos, UErrorCode &success) const
void formatComplexSubMessage (int32_t msgStart, double pluralNumber, const Formattable *arguments, const UnicodeString *argumentNames, int32_t cnt, AppendableWrapper &appendTo, UErrorCode &success) const
const FormattablegetArgFromListByName (const Formattable *arguments, const UnicodeString *argumentNames, int32_t cnt, UnicodeString &name) const
UnicodeString getArgName (int32_t partIndex)
const Formattable::TypegetArgTypeList (int32_t &listCount) const
FormatgetCachedFormatter (int32_t argumentNumber) const
const DateFormatgetDefaultDateFormat (UErrorCode &) const
const NumberFormatgetDefaultNumberFormat (UErrorCode &) const
UnicodeString getLiteralStringUntilNextArgument (int32_t from) const
int32_t nextTopLevelArgStart (int32_t partIndex) const
Formattableparse (int32_t msgStart, const UnicodeString &source, ParsePosition &pos, int32_t &count, UErrorCode &ec) const
void resetPattern ()
void setArgStartFormat (int32_t argStart, Format *formatter, UErrorCode &status)
void setCustomArgStartFormat (int32_t argStart, Format *formatter, UErrorCode &status)
FieldPositionupdateMetaData (AppendableWrapper &dest, int32_t prevLength, FieldPosition *fp, const Formattable *argId) const

Static Private Member Functions

static int32_t findKeyword (const UnicodeString &s, const UChar *const *list)

Private Attributes

int32_t argTypeCapacity
int32_t argTypeCount
Locale fLocale
Format ** formatAliases
int32_t formatAliasesCapacity
UBool hasArgTypeConflicts
MessagePattern msgPattern
PluralSelectorProvider pluralProvider


class MessageFormatAdapter

Detailed Description

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.

A MessageFormat is constructed from a pattern string with arguments in {curly braces} which will be replaced by formatted values.

MessageFormat differs from the other Format classes in that you create a MessageFormat object with one of its constructors (not with a createInstance style factory method). Factory methods aren't necessary because MessageFormat 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 (see usesNamedArguments()).

An argument might not specify any format type. In this case, a Number value is formatted with a default (for the locale) NumberFormat, a Date value is formatted with a default (for the locale) DateFormat, and for any other value its toString() value is used.

An argument might specify a "simple" type for which the specified Format object is created, cached and used.

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.

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.

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 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).

Patterns and Their Interpretation

MessageFormat uses patterns of the following form:

 message = messageText (argument messageText)*
 argument = noneArg | simpleArg | complexArg
 complexArg = choiceArg | pluralArg | selectArg
 noneArg = '{' argNameOrNumber '}'
 simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'
 choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'
 pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'
 selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'
 choiceStyle: see ChoiceFormat
 pluralStyle: see PluralFormat
 selectStyle: see 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
  • 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 apostrohpe 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 also UMessagePatternApostropheMode
  • 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 in MessageFormat. See the annotations for U+0027 Apostrophe in The Unicode Standard.

The argType and argStyle values are used to create a Format 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 argStyleText must be a valid pattern string for the Format subclass used.

argType argStyle resulting Format 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 new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), 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)
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);

Usage Information

Here are some examples of usage: Example 1:

     UErrorCode success = U_ZERO_ERROR;
     GregorianCalendar cal(success);
     Formattable arguments[] = {
         Formattable( (Date) cal.getTime(success), Formattable::kIsDate),
         "a disturbance in the Force"

     UnicodeString result;
          "At {1,time} on {1,date}, there was {2} on planet {0,number}.",
          arguments, 3, result, success );

     cout << "result: " << result << endl;
     //<output>: At 4:34:20 PM on 23-Mar-98, there was a disturbance
     //             in the Force on planet 7.

Typically, the message format will come from resources, and the arguments will be dynamically set at runtime.

Example 2:

     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).

For messages that include plural forms, you can use a plural argument:

  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}\".}}",
  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".

See PluralFormat and PluralRules for details.


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.

ICU 2.0

Definition at line 318 of file msgfmt.h.

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index