This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Intl.ListFormat
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since апрель 2021 г..
ОбъектIntl.ListFormat представляет собой конструктор объектов, включающих языка-зависимое форматирование списков.
In this article
Интерактивный пример
const vehicles = ["Motorcycle", "Bus", "Car"];const formatter = new Intl.ListFormat("en", { style: "long", type: "conjunction",});console.log(formatter.format(vehicles));// Expected output: "Motorcycle, Bus, and Car"const formatter2 = new Intl.ListFormat("de", { style: "short", type: "disjunction",});console.log(formatter2.format(vehicles));// Expected output: "Motorcycle, Bus oder Car"const formatter3 = new Intl.ListFormat("en", { style: "narrow", type: "unit" });console.log(formatter3.format(vehicles));// Expected output: "Motorcycle Bus Car"Синтаксис
new Intl.ListFormat([locales[, options]])
Параметры
localesНеобязательный параметр. Строка с языковой меткой BCP 47 или массив таких строк. Описание общей формы и интерпретации аргумента
localesсмотрите на страницеIntl.optionsНеобязательный параметр. Объект с некоторыми или всеми из следующих свойств:
localeMatcherИспользуемый алгоритм сопоставления локалей. Возможные значения:"lookup"и"best fit"; по умолчанию используется"best fit". Подробнее см. на страницеIntl.typeФормат вывода. Возможные значения:"conjunction"для вывода значений через "и" (используется по умолчанию, прим.A, B и C) или"disjunction"для вывода значений через "или" (прим.A, B или C)."unit"для вывода значений с единицами измерений (прим.5 фунтов, 12 унций).styleСтиль форматирования вывода. Возможные значения:"long"(используется по умолчанию, прим.A, B и C);"short"или"narrow"(прим.A, B, C). При использованииnarrow, параметрtypeможет принимать только значениеunit.
Описание
>Свойства
Intl.ListFormat.prototypeПозволяет добавлять свойства ко всем объектам
Intl.ListFormat
Методы
Intl.ListFormat.supportedLocalesOf()Возвращает массив, содержащий те из переданных ему локалей, которые поддерживаются без необходимости использовать локаль по умолчанию.
Примеры
>Использованиеformat
Пример ниже показывает как создать объектListFormat с поддержкой форматирования на русском языке и получить отформатированную строку с помощью методаformat.
const list = ["Motorcycle", "Bus", "Car"];console.log( new Intl.ListFormat("ru-RU", { style: "long", type: "conjunction" }).format( list, ),);// > Motorcycle, Bus и Carconsole.log( new Intl.ListFormat("ru-RU", { style: "short", type: "disjunction" }).format( list, ),);// > Motorcycle, Bus или Carconsole.log( new Intl.ListFormat("ru-RU", { style: "narrow", type: "unit" }).format(list),);// > Motorcycle Bus CarИспользованиеformatToParts
Пример ниже показывает как получить отформатированные части объектаListFormat с помощью метода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" } ];Спецификации
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification> # listformat-objects> |