unicode-org / message-format-wg

Developing a standard for localizable message strings
Other
231 stars 34 forks source link

Can you create a MF2 pattern with "match" expression in the middle of the pattern? #899

Closed rumpfc closed 1 week ago

rumpfc commented 1 week ago

I experimented a bit with the new MessageFormat 2.0 syntax (using ICU4J 75.1). The pattern is

.match {$count :number} 0 {{You have no new notifications}} one {{You have {$count} new notification}} * {{You have {$count} new notifications}}

I can see how every option starts with You have, so I tried to put that part at the front of the match expression, but that results in a parsing error

You have {.match {$count :number} 0 {{no new notifications}} one {{{$count} new notification}} * {{{$count} new notifications}}}
Message: <<You have {.match {$count :number} 0 {{no new notifications}} one {{{$count} new notification}} * {{{$count} new notifications}}}>>
Error: Parse error [10]: Literal expression expected.
You have {^^^.match {$count :number} 0 {{no new notifications}} one {{{$count} new notification}} * {{{$count} new notifications}}}

What would be the correct syntax in such a situation? Or am I required to write the complete pattern even though start/end parts are the same?

Example Java code

public class Main {
    public static void main(String[] args) {
        String matchPattern1 = ".match {$count :number} 0 {{You have no new notifications}} one {{You have {$count} new notification}} * {{You have {$count} new notifications}}";
        String matchPattern2 = "You have {.match {$count :number} 0 {{no new notifications}} one {{{$count} new notification}} * {{{$count} new notifications}}}";

        final Locale enGb = Locale.forLanguageTag("en-GB");
        Map arguments = new HashMap<>();
        arguments.put("count", 1);

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

        System.out.println(mf2.formatToString(arguments));
    }
}
bearfriend commented 1 week ago

Note that the start and end parts are the same in English. That may not be true in other languages.

From "Why MessageFormat needs a successor"

select and plural placeholders inside a message are difficult to translate as grammatical agreement may require words outside the select/plural to change. See https://en.wikipedia.org/wiki/Agreement_(linguistics)

Translating split messages is already difficult for readability reasons, but this additional agreement issue can make things much worse.

rumpfc commented 1 week ago

Ok, I understand the decision behind this. Makes sense to keep syntax simple. Thanks for letting me know.

I consider using ICU MF2 in a PoC of mine for building automatic messages. Looking forward to your progress.