百科狗-知识改变命运!

Intl.ListFormat - JavaScript Intl 对象

乐乐2年前 (2023-11-21)阅读数 26#技术干货
文章标签可选

Intl.ListFormat - JavaScript Intl 对象

Intl.ListFormat

Intl.ListFormat是一个语言相关的列表格式化构造器。

语法

new Intl.ListFormat([locales[, options]]) 

参数

locales

可选的.。符合 BCP 47 语言标注的字符串或字符串数组。locales参数的一般形式和相关解释,请参阅 Intl page.

options可选的。拥有下面所列属性中任意几个或全部的对象:
  • localeMatcher
    指定要使用的本地匹配算法。可选的值有"lookup""best fit";默认情况下使用"best fit"。该参数的更多信息,请参考Intlpage.
  • type
    消息输出的格式。可选的值有用于替代基于“且”关系列表的"conjunction"(默认值,例如:A, B, and C),或者用于替代基于“或”关系列表的"disjunction"(例如:A, B, or C),以及用于替代带计量单位的值列表的"unit"(例如:5 pounds, 12 ounces).
  • style
    被格式化消息的长度。可选值有:"long"(默认值,例如:A, B, and C)、"short"或者"narrow"(例如:A, B, C)。当style的值为narrow时,type 属性的值只能取值unit。

描述

属性

Intl.ListFormat.prototype允许增加一个属性到列表中的所有对象。

方法

Intl.ListFormat.supportedLocalesOf()返回一个包含指定的被支持区域设置的数组,没有时使用运行环境默认区域设置

示例

使用format

下面的例子展示了用英语语言怎么去创建一个列表格式化器。

const list = ['Motorcycle', 'Bus', 'Car'];

 console.log(new Intl.ListFormat('en-GB', { style: 'long', type: 'conjunction' }).format(list));
// > Motorcycle, Bus and Car

 console.log(new Intl.ListFormat('en-GB', { style: 'short', type: 'disjunction' }).format(list));
// > Motorcycle, Bus or Car

 console.log(new Intl.ListFormat('en-GB', { style: 'narrow', type: 'unit' }).format(list));
// > Motorcycle Bus Car

使用formatToParts

下面的例子展示了如何创建一个返回被格式化部分的列表格式化器。

const list = ['Motorcycle', 'Bus', 'Car'];
console.log(new Intl.ListFormat('en-GB', { style: 'long', type: 'conjunction' }).formatToParts(list));
 
// > [ { "type": "element", "value": "Motorcycle" }, { "type": "literal", "value": ", " }, { "type": "element", "value": "Bus" }, { "type": "literal", "value": ", and " }, { "type": "element", "value": "Car" } ];

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}