ListFormat + PluralRules:列表拼接 & 复数
要拼出一句自然的「You have 3 new messages from Amy, Bob, and Cara」,就同时需要这两个:ListFormat 把数组连成带正确连接词的列表,PluralRules 决定该用 message 还是 messages。
1 · ListFormat:把数组拼成列表
type 决定连接词:conjunction(和/and)、disjunction(或/or)、unit(无连接词,用于「5 ft 7 in」这种)。注意英语 and 前那个 Oxford comma 由 locale 数据决定,不用你操心。
2 · PluralRules:select(n) → 复数类别
英语只有 one(1)和 other(其余),所以 n===1?'item':'items' 在英语里尚可行。但俄语有 one/few/many、阿拉伯语有全部 6 类(zero/one/two/few/many/other)、中日韩只有 other。select(n) 负责查 CLDR
规则、返回一个类别名:
**不要用 select 当查找键去硬编码翻译。**正确做法是为每个类别准备一条消息模板(i18n 库如 ICU MessageFormat / FormatJS 内部正是用 PluralRules),再按 select(n) 选模板。序数(1st / 2nd / 3rd)要用 { type:'ordinal' },和基数规则不同。
3 · 组合:拼一句完整的通知文案
把两者 + NumberFormat 串起来,做一个随 N 变化的通知文案: