For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/4/proposal-intl-list-format.md.
  • 简体中文
  • Intl.ListFormat S4

    提案概览
    提案速览

    该提案引入了 Intl.ListFormat 构造函数,用于实现语言敏感的列表格式化。它提供了 format 和 formatToParts 方法,以控制输出的样式和并列类型。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    Intl.ListFormat API 规范

    概述

    Intl.ListFormat 对象是一个构造函数,用于创建支持语言敏感的列表格式化的对象。

    使用示例

    以下示例展示了如何使用英语创建格式化列表。

    // 在你的区域设置中创建列表格式化器
    // 并显式传入默认值。
    const lf = new Intl.ListFormat("en", {
        localeMatcher: "best fit", // 其他值:"lookup"
        type: "conjunction", // "conjunction", "disjunction" 或 "unit"
        style: "long", // 其他值:"short" 或 "narrow"
    });
    
    lf.format(['Motorcycle', 'Truck' , 'Car']);
    // > "Motorcycle, Truck, and Car"

    实现状态

    阶段 4

    实现进度

    反向链接

    作者

    • Zibi Braniecki (@zbraniecki)

    审阅者

    • Jamund Ferguson (@xjamundx)
    • Daniel Ehrenberg (@littledan)

    参考信息

    本提案基于 LDML 规范的列表模式(List Patterns):

    提案

    规范

    你可以查看 规范文本

    API

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

    Intl.ListFormat 对象是一个构造函数,用于创建支持语言敏感的列表格式化的对象。

    locales

    可选。一个包含 BCP 47 语言标签的字符串,或此类字符串的数组。关于 locales 参数的一般形式和解释,请参见 Intl 页面

    options

    可选。一个对象,包含以下部分或全部属性:

    • localeMatcher

    要使用的区域设置匹配算法。可能的值为 "lookup""best fit";默认值为 "best fit"。关于此选项的信息,请参见 Intl 页面

    • style

    格式化消息的长度。可能的值为:"long"(默认,例如 A, B, and C);"short""narrow"(例如 A, B, C)。当 style 为 narrow 时,unit 是类型选项唯一允许的值。

    • type

    输出消息的格式。可能的值为 "conjunction",表示基于“和”的列表(默认,例如 A, B, and C),或 "disjunction",表示基于“或”的列表(例如 A, B, or C)。"unit" 表示带单位的值的列表(例如 5 pounds, 12 ounces)。

    Intl.ListFormat.prototype.format(list)

    Intl.ListFormat.prototype.format 方法根据此 Intl.ListFormat 对象的区域设置和格式化选项来格式化列表。

    示例
    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
    Intl.ListFormat.prototype.formatToParts(list)

    Intl.ListFormat.prototype.formatToParts() 方法是 format 方法的一个版本,它返回一个对象数组,表示对象的“部分”,将格式化后的列表拆分成其组成部分,并将其与其他周围文本分隔开来。

    示例
    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": "Truck" }, { "type": "literal", "value": ", and " }, { "type": "element", "value": "Car" } ]

    开发

    渲染规范

    npm install
    npm run build
    open out/index.html

    链接