javascriptinternationalizationglobalizationjavascript-globalize

How to validate ICU messages syntax using Globalizejs?


There are a few i18n libraries available in JavaScript and one of the most complete ones seems to be GlobalizeJs.

While testing it, I found a problem when I send invalid ICU messages. It seems to ignore the errors. Examples below:

    Globalize.loadMessages({
        en: {
            test1: [
                "You have {count, plural, one {# hot dog} one {# hamburger} one {# sandwich} other {# snacks}} in your lunch bag."
            ],
            test2: [
                "You have {count, plural, one {# hot dog} thisIsNotValid1 {# hamburger} thisIsNotValid2 {# sandwich} other {# snacks}} in your lunch bag."
            ]
        }
    });

    console.log(Globalize( 'en' ).messageFormatter( 'test1' )({count: 1}));
    // Output: You have 1 sandwich in your lunch bag.
    // Expected output: exception thrown because the plural "one" is used multiple times.
    console.log(Globalize( 'en' ).messageFormatter( 'test2' )({count: 1}));
    // Output: You have 1 hot dog in your lunch bag.
    // Expected output: exception thrown because the plural "thisIsNotValid1" and "thisIsNotValid2" are not valid.

Is there a way to catch that the ICU syntax is invalid instead of silently outputting the best effort result?


Solution

  • It may be possible to use https://github.com/messageformat/messageformat/tree/master/packages/parser (or a modified version of it) to detect the invalid syntax you're looking for

    PS: Thanks for submitting this question here in Stack Overflow and in https://github.com/globalizejs/globalize/issues/874