gpt4 book ai didi

javascript - 如何使用 Globalizejs 验证 ICU 消息语法?

转载 作者:行者123 更新时间:2023-11-30 19:18:35 25 4
gpt4 key购买 nike

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 语法无效而不是默默地输出最大努力的结果?

最佳答案

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

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

关于javascript - 如何使用 Globalizejs 验证 ICU 消息语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57734196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com