MessageFormat class abstract final

Fills arguments into a message pattern such as Hello {0}!, using the syntax of Java's MessageFormat, which message files are written in.

Message sources call it for every message with arguments, so the syntax below is what you can write in messages.properties. Call it directly to format a pattern that does not come from a message source.

Supported: {0} placeholders; quoting ('' is a quote, text inside single quotes is literal, a } outside a placeholder is literal); {0,number} (#,##0.###), {0,number,integer}, {0,number,percent}, {0,number,<pattern>} (DecimalFormat patterns: #, 0, , grouping, . fraction, quoted and plain literal prefixes and suffixes, %, a ;negative subpattern such as #,##0.00;(#,##0.00)); {0,date} and {0,time} with the styles short, medium (the default), long, full or a SimpleDateFormat pattern ({0,date,yyyy-MM-dd}); {0,choice,0#none|1#one|1<{0} many}. Numbers are rounded half-even on their exact binary value, like Java's DecimalFormat (0.15 with #.# gives 0.1: the double is slightly below 0.15). Missing arguments stay as {n}. As with Java's applyPattern, the whole pattern is checked: a malformed element throws even when its argument is missing or of another type.

With a format locale (de, fr_CA, ...), numbers use its decimal and grouping separators (1.234,5 in German, 1 234,5 in French); without one, US symbols.

MessageFormat.format('Hello {0}, {1}!', ['Ada', 'Linus']);
// Hello Ada, Linus!
MessageFormat.format("It''s {0,number,#.##}", [3.14159]); // It's 3.14
MessageFormat.format(
  'There {0,choice,0#are no files|1#is one file|1<are {0} files}.',
  [1234],
); // There are 1,234 files.
MessageFormat.format('{0,number}', [1234.5], locale: 'de'); // 1.234,5
MessageFormat.format('{0,date,short}', [DateTime(2026, 1, 12)]); // 1/12/26

Spring (Java) uses java.text.MessageFormat:

MessageFormat.format("Hello {0}, {1}!", "Ada", "Linus");
new MessageFormat("It''s {0,number,#.##}", Locale.US)
    .format(new Object[] {3.14159});

Differences from Java: {0,number,percent} and currency keep the US layout (26%; currency gives two decimals without a symbol); the minus sign is always -; dates and times use the US English styles and names whatever the locale (Jan 12, 2026, 3:30 PM with a plain space where JDK 20+ prints a narrow no-break space), and a time zone is the DateTime's (UTC or the local zone's name); no scientific notation (0.##E0).

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

format(String pattern, List<Object?> args, {String? locale}) → String
Returns pattern with args filled in; numbers use the decimal and grouping separators of locale (US ones without it).