Class MessageFormatter
MessageFormatter
is the next iteration of MessageFormat
.
This new version builds on what we learned from using MessageFormat
for 20 years
in various environments, either exposed "as is" or as a base for other public APIs.
It is more modular, easier to backport, and provides extension points to add new formatters and selectors without having to modify the specification.
We will be able to add formatters for intervals, relative times, lists, measurement units, people names, and more, and support custom formatters implemented by developers outside of ICU itself, for company or even product specific needs.
MessageFormat 2 will support more complex grammatical features, such as gender, inflections, and tagging parts of the message for style changes or speech.
The reasoning for this effort is shared in the “Why MessageFormat needs a successor” document.
The “MessageFormat 2” project, 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 tech preview implements enough of the MessageFormat
functions to be useful,
but the final set of functions and the parameters accepted by those functions is not yet finalized.
These are the functions interpreted right now:
datetime |
Similar to the ICU "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 SimpleDateFormat .
| |
number |
Similar to the ICU "number". |
skeleton These are the number skeletons as supported by 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 function will support skeletons, option backs, or both. | |
offset Used to support plural with an offset. | |
identity | Returns the direct string value of the argument (calling toString() ). |
plural |
Similar to the ICU "plural" . |
skeleton These are the number skeletons as supported by 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 the ICU "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. |
select | Literal match, same as the ICU4 "select" . |
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Deprecated.This API is for technology preview only. -
Method Summary
Modifier and TypeMethodDescriptionstatic MessageFormatter.Builder
builder()
Deprecated.This API is for technology preview only.Deprecated.This API is for technology preview only.formatToString
(Map<String, Object> arguments) Deprecated.This API is for technology preview only.Deprecated.This API is for technology preview only.Deprecated.This API is for technology preview only.Deprecated.This API is for technology preview only.
-
Method Details
-
builder
Deprecated.This API is for technology preview only.Creates a builder.- Returns:
- the Builder.
-
getLocale
Deprecated.This API is for technology preview only.Get the locale to use for all the formatting and selections in the currentMessageFormatter
.- Returns:
- the locale.
-
getPattern
Deprecated.This API is for technology preview only.Get the pattern (the serialized message in MessageFormat 2 syntax) of the currentMessageFormatter
.If the
MessageFormatter
was created from anMf2DataModel
the this string is generated from that model.- Returns:
- the pattern.
-
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
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:
- the data model.
-
formatToString
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
- a map of objects to be formatted and substituted.- Returns:
- the string representing the message with parameters replaced.
- Throws:
IllegalArgumentException
- when something goes wrong (for example wrong argument type, or null arguments, etc.)
-
format
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
- a map of objects to be formatted and substituted.- Returns:
- the
FormattedMessage
class representing the message with parameters replaced.
-