Top Description Inners Fields Constructors Methods
com.ibm.icu.message2

public Class MessageFormatter

extends Object
Class Inheritance
Annotations
@Deprecated
Imports
java.util.Locale, .Map

Deprecated

This API is for technology preview only.

Overview of MessageFormatter

In ICU4J, the MessageFormatter class is the next iteration of com.ibm.icu.text.MessageFormat. This new version will build on the lessons learned from using MessageFormat for 25 years in various environments, when used directly or as a base for other public APIs.

The effort to design a succesor to MessageFormat will result in a specification referred to as MessageFormat 2.0. The reasoning for this effort is shared in the “Why MessageFormat needs a successor” document.

MessageFormat 2.0 will be more modular and easier to port and backport. It will also provide extension points via interfaces to allow users to supply new formatters and selectors without having to modify the specification. ICU will eventually include support for new formatters, such as intervals, relative time, lists, measurement units, personal names, and more, as well as the ability for users to supply their own custom implementations. These will potentially support use cases like grammatical gender, inflection, markup regimes (such as those require for text-to-speech), and other complex message management needs.

The MessageFormat Working Group, which develops the new data model, semantics, and syntax, is hosted on GitHub. The current specification for the syntax and data model can be found here.

This technical preview implements enough functions for MessageFormatter to be useful in many situations, but the final set of functions and the parameters accepted by those functions is not yet finalized.

Examples

Basic usage

import static org.junit.Assert.assertEquals;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.ibm.icu.message2.MessageFormatter;

@Test
public void testMf2() {
    final Locale enGb = Locale.forLanguageTag("en-GB");
    Map arguments = new HashMap<>();
    arguments.put("name", "John");
    arguments.put("exp", new Date(1679971371000L));  // March 27, 2023, 7:42:51 PM

    MessageFormatter mf2 = MessageFormatter.builder()
        .setPattern("{Hello {$name}, your card expires on {$exp :datetime skeleton=yMMMdE}!}")
        .setLocale(enGb)
        .build();

    assertEquals(
        "Hello John, your card expires on Mon, 27 Mar 2023!",
        mf2.formatToString(arguments));
}

Placeholder examples

Code to set runtime value for placeholder Examples of placeholder in message pattern
arguments.put("name", "John") }$name~
arguments.put("exp", new Date(…)) }$exp :datetime skeleton=yMMMdE~
}$exp :datetime datestyle=full~
arguments.put("val", 3.141592653) }$val~
}$val :number skeleton=(.####)~
No argument for fixed values known at build time }(123456789.531) :number~

Plural selection message

@Test
public void testMf2Selection() {
   final String message = "match {$count :plural}\n"
           + " when one {You have one notification.}\n"
           + " when * {You have {$count} notifications.}\n";
   final Locale enGb = Locale.forLanguageTag("en-GB");
   Map arguments = new HashMap<>();

   MessageFormatter mf2 = MessageFormatter.builder()
       .setPattern(message)
       .setLocale(enGb)
       .build();

   arguments.put("count", 1);
   assertEquals(
       "You have one notification.",
       mf2.formatToString(arguments));

   arguments.put("count", 42);
   assertEquals(
       "You have 42 notifications.",
       mf2.formatToString(arguments));
}

Built-in formatter functions

The tech preview implementation comes with formatters for numbers (number), date / time (datetime), plural selectors (plural and selectordinal), and general selector (select), very similar to what MessageFormat offers.

The ICU test code covers most features, and has examples of how to make custom placeholder formatters; you can look for classes that implement com.ibm.icu.message2.FormatterFactory (they are named Custom*Test.java).

Functions currently implemented

These are the functions interpreted right now:

datetime Similar to MessageFormat's date and time.
datestyle and timestyle
Similar to argStyle : short | medium | long | full.
Same values are accepted, but we can use both in one placeholder, for example {$due :datetime datestyle=full timestyle=long}.
pattern
Similar to argStyle = argStyleText.
This is bad i18n practice, and will probably be dropped.
This is included just to support migration to MessageFormat 2.
skeleton
Same as argStyle = argSkeletonText.
These are the date/time skeletons as supported by com.ibm.icu.text.SimpleDateFormat.
number Similar to MessageFormat's number.
skeleton
These are the number skeletons as supported by com.ibm.icu.number.NumberFormatter.
minimumFractionDigits
Only implemented to be able to pass the unit tests from the ECMA tech preview implementation, which prefers options bags to skeletons.
TBD if the final {@number } function will support skeletons, option backs, or both.
offset
Used to support plural with an offset.
identityReturns the direct string value of the argument (calling toString()).
plural Similar to MessageFormat's plural.
skeleton
These are the number skeletons as supported by com.ibm.icu.number.NumberFormatter.
Can also be indirect, from a local variable of type number (recommended).
offset
Used to support plural with an offset.
Can also be indirect, from a local variable of type number (recommended).
selectordinal Similar to MessageFormat's selectordinal.
For now it accepts the same parameters as plural, although there is no use case for them.
TBD if this will be merged into plural (with some kind option) or not.
selectLiteral match, same as MessageFormat's select.
ICU Status
Internal ICU component: 72 technology preview

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MessageFormatter.Builder

Deprecated This API is for technology preview only.
A Builder used to build instances of MessageFormatter.

Field Summary

Modifier and TypeField and Description
private final Mf2DataModel
dataModel

Deprecated as a consequence of Mf2DataModel being deprecated.
private final Mf2FunctionRegistry
functionRegistry

Deprecated as a consequence of Mf2FunctionRegistry being deprecated.
private final Locale
private final Mf2DataModelFormatter
private final String

Constructor Summary

AccessConstructor and Description
private
MessageFormatter(MessageFormatter.Builder builder)

Deprecated as a consequence of MessageFormatter.Builder being deprecated.

Method Summary

Modifier and TypeMethod and Description
public static MessageFormatter.Builder

Returns:

the Builder.
builder
()

Deprecated This API is for technology preview only.
Creates a builder.
public FormattedMessage

Returns:

the FormattedMessage class representing the message with parameters replaced.
format
(Map<String, Object>
a map of objects to be formatted and substituted.
arguments
)

Deprecated This API is for technology preview only.
Not yet implemented: formats a map of objects by iterating over the MessageFormat's pattern, with the plain text “as is” and the arguments replaced by the formatted objects.
public String

Returns:

the string representing the message with parameters replaced.
formatToString
(Map<String, Object>
a map of objects to be formatted and substituted.
arguments
)

Deprecated This API is for technology preview only.
Formats a map of objects by iterating over the MessageFormat's pattern, with the plain text “as is” and the arguments replaced by the formatted objects.
public Mf2DataModel

Returns:

the data model.
getDataModel
()

Deprecated This API is for technology preview only.
Give public access to the message data model.
public Locale

Returns:

the locale.
getLocale
()

Deprecated This API is for technology preview only.
Get the locale to use for all the formatting and selections in the current MessageFormatter.
public String

Returns:

the pattern.
getPattern
()

Deprecated This API is for technology preview only.
Get the pattern (the serialized message in MessageFormat 2 syntax) of the current MessageFormatter.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataModelback to summary
private final Mf2DataModel dataModel

Deprecated

as a consequence of Mf2DataModel being deprecated. See corresponding docs for further information.

functionRegistryback to summary
private final Mf2FunctionRegistry functionRegistry

Deprecated

as a consequence of Mf2FunctionRegistry being deprecated. See corresponding docs for further information.

localeback to summary
private final Locale locale
modelFormatterback to summary
private final Mf2DataModelFormatter modelFormatter
patternback to summary
private final String pattern

Constructor Detail

MessageFormatterback to summary
private MessageFormatter(MessageFormatter.Builder builder)

Deprecated

as a consequence of MessageFormatter.Builder being deprecated. See corresponding docs for further information.

Method Detail

builderback to summary
public static MessageFormatter.Builder builder()

Deprecated

This API is for technology preview only.

Creates a builder.

Returns:MessageFormatter.Builder

the Builder.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
formatback to summary
public FormattedMessage format(Map<String, Object> arguments)

Deprecated

This API is for technology preview only.

Not yet implemented: formats a map of objects by iterating over the MessageFormat's pattern, with the plain text “as is” and the arguments replaced by the formatted objects.

Parameters
arguments:Map<String, Object>

a map of objects to be formatted and substituted.

Returns:FormattedMessage

the FormattedMessage class representing the message with parameters replaced.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
formatToStringback to summary
public String formatToString(Map<String, Object> arguments)

Deprecated

This API is for technology preview only.

Formats a map of objects by iterating over the MessageFormat's pattern, with the plain text “as is” and the arguments replaced by the formatted objects.

Parameters
arguments:Map<String, Object>

a map of objects to be formatted and substituted.

Returns:String

the string representing the message with parameters replaced.

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
when something goes wrong (for example wrong argument type, or null arguments, etc.)
ICU Status
Internal ICU component: 72 technology preview
getDataModelback to summary
public Mf2DataModel getDataModel()

Deprecated

This API is for technology preview only.

Give public access to the message data model.

This data model is similar to the functionality we have today in com.ibm.icu.text.MessagePatternUtil maybe even a bit more higher level.

We can also imagine a model where one parses the string syntax, takes the data model, modifies it, and then uses that modified model to create a MessageFormatter.

Returns:Mf2DataModel

the data model.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
getLocaleback to summary
public Locale getLocale()

Deprecated

This API is for technology preview only.

Get the locale to use for all the formatting and selections in the current MessageFormatter.

Returns:Locale

the locale.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
getPatternback to summary
public String getPattern()

Deprecated

This API is for technology preview only.

Get the pattern (the serialized message in MessageFormat 2 syntax) of the current MessageFormatter.

If the MessageFormatter was created from an Mf2DataModel the this string is generated from that model.

Returns:String

the pattern.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
com.ibm.icu.message2 back to summary

public Class MessageFormatter.Builder

extends Object
Class Inheritance
Annotations
@Deprecated

Deprecated

This API is for technology preview only.

A Builder used to build instances of MessageFormatter.
ICU Status
Internal ICU component: 72 technology preview

Field Summary

Modifier and TypeField and Description
private Mf2DataModel
dataModel

Deprecated as a consequence of Mf2DataModel being deprecated.
private Mf2FunctionRegistry
functionRegistry

Deprecated as a consequence of Mf2FunctionRegistry being deprecated.
private Locale
private String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public MessageFormatter

Returns:

the MessageFormatter created.
build
()

Deprecated This API is for technology preview only.
Builds an instance of MessageFormatter.
public MessageFormatter.Builder

Returns:

the builder, for fluent use.
setDataModel
(Mf2DataModel
the pattern to set.
dataModel
)

Deprecated This API is for technology preview only.
Sets the data model used to create the message.
public MessageFormatter.Builder

Returns:

the builder, for fluent use.
setFunctionRegistry
(Mf2FunctionRegistry
the function registry to set.
functionRegistry
)

Deprecated This API is for technology preview only.
Sets an instance of Mf2FunctionRegistry that should register any custom functions used by the message.
public MessageFormatter.Builder

Returns:

the builder, for fluent use.
setLocale
(Locale
the locale to set.
locale
)

Deprecated This API is for technology preview only.
Sets the locale to use for all formatting and selection operations.
public MessageFormatter.Builder

Returns:

the builder, for fluent use.
setPattern
(String
the pattern to set.
pattern
)

Deprecated This API is for technology preview only.
Sets the pattern (in MessageFormat 2 syntax) used to create the message.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataModelback to summary
private Mf2DataModel dataModel

Deprecated

as a consequence of Mf2DataModel being deprecated. See corresponding docs for further information.

functionRegistryback to summary
private Mf2FunctionRegistry functionRegistry

Deprecated

as a consequence of Mf2FunctionRegistry being deprecated. See corresponding docs for further information.

localeback to summary
private Locale locale
patternback to summary
private String pattern

Constructor Detail

Builderback to summary
private Builder()

Method Detail

buildback to summary
public MessageFormatter build()

Deprecated

This API is for technology preview only.

Builds an instance of MessageFormatter.

Returns:MessageFormatter

the MessageFormatter created.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
setDataModelback to summary
public MessageFormatter.Builder setDataModel(Mf2DataModel dataModel)

Deprecated

This API is for technology preview only.

Sets the data model used to create the message.
It conflicts with the pattern, so it will reset it (the last call on setter wins).

Parameters
dataModel:Mf2DataModel

the pattern to set.

Returns:MessageFormatter.Builder

the builder, for fluent use.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
setFunctionRegistryback to summary
public MessageFormatter.Builder setFunctionRegistry(Mf2FunctionRegistry functionRegistry)

Deprecated

This API is for technology preview only.

Sets an instance of Mf2FunctionRegistry that should register any custom functions used by the message.

There is no need to do this in order to use standard functions (for example date / time / number formatting, plural / ordinal / literal selection).
The exact set of standard functions, with the types they format and the options they accept is still TBD.

Parameters
functionRegistry:Mf2FunctionRegistry

the function registry to set.

Returns:MessageFormatter.Builder

the builder, for fluent use.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
setLocaleback to summary
public MessageFormatter.Builder setLocale(Locale locale)

Deprecated

This API is for technology preview only.

Sets the locale to use for all formatting and selection operations.

Parameters
locale:Locale

the locale to set.

Returns:MessageFormatter.Builder

the builder, for fluent use.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview
setPatternback to summary
public MessageFormatter.Builder setPattern(String pattern)

Deprecated

This API is for technology preview only.

Sets the pattern (in MessageFormat 2 syntax) used to create the message.
It conflicts with the data model, so it will reset it (the last call on setter wins).

Parameters
pattern:String

the pattern to set.

Returns:MessageFormatter.Builder

the builder, for fluent use.

Annotations
@Deprecated
ICU Status
Internal ICU component: 72 technology preview