We're updating the issue view to help you get more done. 

Improve usability of message format

Description

1. It is clumsy now to use named arguments, because you have to put together a map (and unfortunately Java doesn't chain those!).

OLD

1 2 3 4 Map<String,Object> arguments = new HashMap(); arguments.put("TIME", t); arguments.put("PLACE", p); String formatted = mf.format(arguments);

We could improve the usability by adding a convenience methods formatPairs(), allowing the following:

NEW

1 2 3 4 5 String formatted = mf.formatPairs("TIME", t, "PLACE", p); // or the static version String formatted = MessageFormat.formatPairs("The office closes on {DATE,date} at {TIME,time}.", ULocale.US, "TIME", t, "PLACE", p);

2. We encourage the use of skeletons, since they offer much more flexibility, but they are also clumsy to use with MessageFormats.

OLD

1 2 3 MessageFormat mf = new MessageFormat("The office closes on {DATE,date} at {TIME,time}.", ULocale.US); mf.setFormatByArgumentName("DATE", DateFormat.getPatternInstance("MMMMd", ULocale.US)); mf.setFormatByArgumentName("TIME", DateFormat.getPatternInstance("jm", ULocale.US));

Moreover, this defeats the purpose a bit, by not encapsulating the format in the message pattern.

We could improve the usability by adding a new argType "skeleton", allowing the following:

NEW

1 MessageFormat mf = new MessageFormat("The office closes on {DATE,MMMMd} at {TIME,jm}.", ULocale.US);

The skeleton is either a datetime skeleton or a number pattern. The characters don't overlap between them.

1 2 3 4 5 6 7 public static UFormat getFormat(String dateOrNumberSkeleton, ULocale locale) { if (SKELETON_CHARACTERS.containsAll(dateOrNumberSkeleton)) { return DateFormat.getPatternInstance(dateOrNumberSkeleton, locale); } else { return new DecimalFormat(dateOrNumberSkeleton, new DecimalFormatSymbols(locale)); } }

It is theoretically possible for a skeleton to consist of exactly the characters used by another argType (like "date"). But they would have to be in the right order, so in practice there'd be no real chance. We could just document it.


3. Static methods useless.

We have the following:

1 2 MessageFormat.format(pattern, argumentArray); MessageFormat.format(pattern, argumentMap);

But that uses the default locale, which is almost always wrong. We should add the following:

1 2 MessageFormat.format(pattern, uLocale, argumentArray); MessageFormat.format(pattern, uLocale, argumentMap);

Status

Assignee

Markus Scherer

Reporter

Mark Davis

Time Needed

Days

Components

Priority

assess