handle basic argument substitution in MessageFormat.format

Thanks to Remi for an initial version of this patch.
This commit is contained in:
Joel Dice 2012-08-11 08:56:21 -06:00
parent e2416ddb85
commit e2ff771baa
2 changed files with 15 additions and 2 deletions

View File

@ -28,8 +28,14 @@ public class MessageFormat extends Format {
public StringBuffer format(Object[] args, StringBuffer target,
FieldPosition p)
{
// todo
return target.append(pattern);
// todo: handle other format substitutions and escapes, and make
// this more efficient:
String result = pattern;
int length = args.length;
for (int i = 0; i < length; i++) {
result = result.replace("{" + i + "}", String.valueOf(args[i]));
}
return target.append(result);
}
public StringBuffer format(Object args, StringBuffer target, FieldPosition p)

View File

@ -139,5 +139,12 @@ public class Strings {
testDecode(false);
testDecode(true);
expect
(java.text.MessageFormat.format
("{0} enjoy {1} {2}. do {4}? {4} do?",
"I", "grape", "nuts", "foobar",
new Object() { public String toString() { return "you"; } })
.equals("I enjoy grape nuts. do you? you do?"));
}
}