ICU 表达式 *select* 未检测到空值

ICU expression *select* not detecting empty value

我正在使用 i18n angular 7 翻译我的应用程序,但我在使用 ICU 表达式时遇到问题 select。问题是我希望条件能够识别并清空这样的值:animal: string

<div i18n="@@example> 
    { animal, select, EMPTY { Cat } dog { Dog } other { Pig } }
</div>

我试过使用

{ animal, select, undefined { Cat } dog { Dog } other { Pig } }

但是没用

当动物是:

(空字符串,null 或未定义) -> 翻译 Cat

-> 翻译狗

其他 -> 翻译猪

不幸的是,select 的每个翻译案例的键似乎被视为字符串,而不是表达式,因此您的 undefined 被视为 "undefined",并且EMPTY 作为 "EMPTY".

作为解决方法,您可以将 animal + "" 评估为您的 select 条件。 undefined + "" === "undefined",它将匹配 "undefined" 在你的 select 个案例中:

{ animal + "", select, undefined { Cat } dog { Dog } other { Pig } }

就是说,您可以将 undefined 转换为您认为不会实际出现在 animal 变量中的任何字符串值以获得相同的结果:

{ animal === undefined? "geranium" : animal , select, geranium { Cat } dog { Dog } other { Pig } }