gpt4 book ai didi

reactjs - FormatJS/React-intl 中的 If/else

转载 作者:行者123 更新时间:2023-12-02 01:59:22 26 4
gpt4 key购买 nike

是否可以在 FormatJS 消息中使用 if/else?

例子

我有一个 bool 变量 isDay 应该确定显示什么消息。当 true 时,我想显示“Day”字样,当 false 时,我想显示 Night

// message string
message = "Day";

// React component
<FormattedMessage
id="message"
values={{isDay: true}}
/>

我希望能够做这样的事情:

message = "{if isDay}Day{else}Night{endif}";

我知道以上不是实际的语法,但想知道 FormatJS 是否可以实现类似的语法?

最佳答案

使用 ICU 消息找到解决方案 select语法。

message = "{isDay, select, true {Day} other {Night}}";

关于reactjs - FormatJS/React-intl 中的 If/else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69181464/

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