generaltranslation@7.8.0
概述
在 generaltranslation@7.8.0 中,我们引入了列表格式化能力,为后续推出的组件和方法打下基础。此版本为 GT 类和独立函数两种形式都新增了 formatListToParts()。
动机
条目列表需要根据区域设置进行正确格式化。具体来说,我们希望支持将多个条目 (而不只是字符串) 组合成格式化列表。这样一来,我们就能更方便地创建任意数据的列表,例如 JSX 元素。
用法
新的 formatListToParts() 函数会在插入适当的特定于区域设置的分隔符的同时,保留列表项的原始类型:
import { GT, formatListToParts } from 'generaltranslation'
const gt = new GT({ locales: ['en'] })
// 混合数据类型
gt.formatListToParts(['apple', 42, { type: 'fruit' }])
// 返回:['apple', ', ', 42, ', and ', { type: 'fruit' }]
// 独立函数
formatListToParts(['red', 'green', 'blue'], {
locales: ['es'],
type: 'disjunction',
})
// 返回:['red', ', ', 'green', ' o ', 'blue']支持多种列表类型和样式:
// 析取(或)
gt.formatListToParts(['A', 'B', 'C'], { type: 'disjunction' })
// 返回:['A', ', ', 'B', ', or ', 'C']
// 短格式
gt.formatListToParts(['first', 'second'], { style: 'short' })
// 返回:['first', ' & ', 'second']
// 单位格式化
gt.formatListToParts(['1km', '2mi'], { type: 'unit' })
// 返回:['1km', ', ', '2mi']组件支持基础
此功能为即将推出的 <List/> 组件以及 msg.list() / gt.list() 方法打下了基础;这些接口将为列表格式化提供 React 形式的支持。