为什么我的嵌套复数 ICU 消息在 react-intl FormattedMessage 中不起作用?

Why my nested plural ICU message does not work in react-intl FormattedMessage?

我正在使用 react-intl 及其 <FormattedMessage /> 标签。

我想要一个结构化的消息,它会根据提供的值选择正确的复数变体,以允许翻译人员使用他们的语言规则,即,如果他们有 "one" 的不同变体,"two"、"many"、项目等。我不想通过 switch 语句将其硬编码到应用程序业务逻辑中,这些语句仅使用 "zero"、"one", 和 "other".

<FormattedMessage id="myMessage" values={{applesCount: 4, orangesCount: 0, pearsCount: 1}} /> 应该从以下来源生成 I have some apples and some pears

由于某些原因,它 returns I have some apples, some pears, and some oranges

{applesCount, plural, 
    zero {{pearsCount, plural, 
        zero {{orangesCount, plural, 
            zero {I have no fruit}
            other {I have some oranges}
        }}
        other {{orangesCount, plural, 
            zero {I have some pears}
            other {I have some pears and some oranges}
        }}
    }}
    other {{pearsCount, plural, 
        zero {{orangesCount, plural, 
            zero {I have some apples}
            other {I have some apples and some oranges}
        }}
        other {{orangesCount, plural, 
            zero {I have some apples and some pears}
            other {I have some apples, some pears, and some oranges}
        }}
    }}
}

我通过https://format-message.github.io/icu-message-format-for-translators/editor.html

测试了它

另外,我有这个代码框,你可以在那里修改它:https://codesandbox.io/s/react-intl-formattedmessage-using-plural-x8ki5

作为参考,我检查了 http://userguide.icu-project.org/formatparse/messages and https://formatjs.io/guides/message-syntax/ 并希望我的消息结构得到支持。

你能帮我检测出哪里出了问题,或者我应该如何更改它才能使其正常工作?

问题是:

English as a language doesn't have grammar specialized specifically for zero number of items.

主要是单数或复数(在一些罕见的残差情况下dual)。

您使用的语法专门针对那些语法专门针对零个项目的语言。 (例如阿拉伯语和拉脱维亚语)

阅读此处:https://formatjs.io/guides/message-syntax/#plural-format
此外,wikipedia 上的这篇文章解释了相同的

因此,该方法不适用于英语。相反,您需要使用 =0(=value 语法)将数量匹配为零以使解决方案生效。

{applesCount, plural, 
    =0 {{pearsCount, plural, 
        =0 {{orangesCount, plural, 
            =0 {I have no fruit}
            other {I have some oranges}
        }}
        other {{orangesCount, plural, 
            =0 {I have some pears}
            other {I have some pears and some oranges}
        }}
    }}
    other {{pearsCount, plural, 
        =0 {{orangesCount, plural, 
            =0 {I have some apples}
            other {I have some apples and some oranges}
        }}
        other {{orangesCount, plural, 
            =0 {I have some apples and some pears}
            other {I have some apples, some pears, and some oranges}
        }}
    }}
}

同样,对于 1 个数字,one 将不适用于英语。您必须使用 =value 语法 (=1).
sandbox 上试过了,效果很好。

希望对您有所帮助。如有任何疑问,请回复。