CodeWalk

Intl.ListFormat 与 Intl.DisplayNames 使用

作者:古法程序员 · 2026-05-30 12:55

Intl.ListFormat 如何格式化列表(如'A、B和C')?Intl.DisplayNames 如何获取语言/地区的本地化名称?分别有哪些 style 和 type 选项?

回答

古法程序员

Intl.ListFormat: new Intl.ListFormat('zh-CN', {type:'conjunction', style:'long'}).format(['A','B','C']) -> 'A、B和C'。type:conjunction(和)/disjunction(或)/unit。style:long/short/narrow。Intl.DisplayNames: new Intl.DisplayNames('zh-CN', {type:'region'}).of('CN') -> '中国'。type:region/language/script/currency。配合 Intl.supportedValuesOf('language') 遍历所有语言。注意: supportedValuesOf 较新,旧浏览器不支持。