Intl.RelativeTimeFormat中narrow与short样式的区别是什么?
关于
Intl.RelativeTimeFormat中style的narrow与short区别 narrow是比short更极致紧凑的格式,它会尽可能压缩文本长度,甚至省略空格或使用最精简的缩写,不同语言的本地化表现差异会更明显。
以下用代码示例对比两者差异(覆盖英文、中文场景):
英文场景示例
const rtfShort = new Intl.RelativeTimeFormat('en', { style: 'short' }); const rtfNarrow = new Intl.RelativeTimeFormat('en', { style: 'narrow' }); console.log(rtfShort.format(-1, 'hour')); // "1 hr ago" console.log(rtfNarrow.format(-1, 'hour')); // "1h ago" console.log(rtfShort.format(3, 'day')); // "in 3 days" console.log(rtfNarrow.format(3, 'day')); // "in 3d"
中文场景示例
const rtfShort = new Intl.RelativeTimeFormat('zh-CN', { style: 'short' }); const rtfNarrow = new Intl.RelativeTimeFormat('zh-CN', { style: 'narrow' }); console.log(rtfShort.format(2, 'month')); // "2个月后" console.log(rtfNarrow.format(2, 'month')); // "2月后" console.log(rtfShort.format(-1, 'year')); // "1年前" console.log(rtfNarrow.format(-1, 'year')); // "1年前"(部分中文场景narrow与short表现一致,取决于本地化规则)
核心差异总结:
short是常规简洁格式,保留必要空格和通用缩写,比如英文的"hr"、中文的量词"个"narrow是极致压缩格式,会进一步精简符号或结构,比如英文直接用单字母后缀"h""d",中文省略冗余量词,部分语言甚至会合并前置词与缩写为无空格形式
内容的提问来源于stack exchange,提问作者Dharman
相关产品推荐
相关产品推荐

