MessageFormat provides functionality to escape '{' and '}' which are reserved chars for a MessageFormat pattern. However, escaping these quotes never works for a ChoiceFormat subformat, since MessageFormat utilizes a recursive implementation; the recursed MessageFormat re-parses the already parsed pattern (which no longer has the escaped quotes), treating escaped brackets as non-escaped brackets.
Actual
jshell> var m = new MessageFormat("{0,choice,0#foo|1#bar|2#Longer foo '{0}' {0} bar baz}")
jshell> $m.format(new Object[]{2})
$9 ==> "Longer foo 2 2 bar baz"
Expected
I would expect "Longer foo {0} 2 bar baz"
Another example is
jshell> var m = new MessageFormat("bar = {0,choice,0#foo '{' {0} bar }");
jshell> $m.format(new Object[]{2})
Throws IAE ("unmatched braces ...")
I would expect that it returns "bar =foo { 2 bar", but instead it throws an IAE because the first quoted bracket is parsed as an unquoted bracket, creating uneven brackets which is the cause for the exception.
Actual
jshell> var m = new MessageFormat("{0,choice,0#foo|1#bar|2#Longer foo '{0}' {0} bar baz}")
jshell> $m.format(new Object[]{2})
$9 ==> "Longer foo 2 2 bar baz"
Expected
I would expect "Longer foo {0} 2 bar baz"
Another example is
jshell> var m = new MessageFormat("bar = {0,choice,0#foo '{' {0} bar }");
jshell> $m.format(new Object[]{2})
Throws IAE ("unmatched braces ...")
I would expect that it returns "bar =foo { 2 bar", but instead it throws an IAE because the first quoted bracket is parsed as an unquoted bracket, creating uneven brackets which is the cause for the exception.
- relates to
-
JDK-8323699 MessageFormat.toPattern() generates non-equivalent MessageFormat pattern
-
- Closed
-