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 April 2021.
Đối tượng Intl.ListFormat cho phép định dạng danh sách nhạy cảm ngôn ngữ.
Try it
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"
Constructor
Intl.ListFormat()-
Tạo một đối tượng
Intl.ListFormatmới.
Phương thức tĩnh
Intl.ListFormat.supportedLocalesOf()-
Trả về một mảng chứa các ngôn ngữ được hỗ trợ trong số các ngôn ngữ đã cung cấp mà không cần phải quay lại ngôn ngữ mặc định của runtime.
Thuộc tính instance
Các thuộc tính này được định nghĩa trên Intl.ListFormat.prototype và được chia sẻ bởi tất cả các instance Intl.ListFormat.
Intl.ListFormat.prototype.constructor-
Hàm constructor đã tạo ra đối tượng instance. Đối với các instance
Intl.ListFormat, giá trị khởi tạo là constructorIntl.ListFormat. Intl.ListFormat.prototype[Symbol.toStringTag]-
Giá trị khởi tạo của thuộc tính
[Symbol.toStringTag]là chuỗi"Intl.ListFormat". Thuộc tính này được dùng trongObject.prototype.toString().
Phương thức instance
Intl.ListFormat.prototype.format()-
Trả về một chuỗi biểu diễn đặc thù theo ngôn ngữ các phần tử của danh sách.
Intl.ListFormat.prototype.formatToParts()-
Trả về một mảng các đối tượng biểu diễn các thành phần khác nhau có thể dùng để định dạng danh sách các giá trị theo cách nhạy cảm ngôn ngữ.
Intl.ListFormat.prototype.resolvedOptions()-
Trả về một đối tượng mới với các thuộc tính phản ánh các tùy chọn ngôn ngữ và kiểu định dạng được tính toán trong quá trình xây dựng đối tượng
Intl.ListFormathiện tại.
Ví dụ
>Sử dụng format
Ví dụ sau cho thấy cách tạo một bộ định dạng danh sách sử dụng ngôn ngữ tiếng Anh.
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
Sử dụng formatToParts
Ví dụ sau cho thấy cách tạo một bộ định dạng danh sách trả về các phần được định dạng
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" } ];
Đặc tả kỹ thuật
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification> # listformat-objects> |