2

JavaScript 中有一些 i18n 库可用,其中最完整的库之一似乎是GlobalizeJs

在测试它时,我在发送无效的 ICU 消息时发现了一个问题。它似乎忽略了错误。以下示例:

    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.

有没有办法捕捉到 ICU 语法无效,而不是默默地输出尽力而为的结果?

4

1 回答 1

2

可以使用https://github.com/messageformat/messageformat/tree/master/packages/parser(或它的修改版本)来检测您正在寻找的无效语法

PS:感谢您在 Stack Overflow 和https://github.com/globalizejs/globalize/issues/874中提交此问题

于 2019-09-03T13:12:07.690 回答