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.
O objeto Intl.ListFormat
habilita a formatação de lista de acordo com o idioma.
Experimente
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"
Construtor
Intl.ListFormat()
-
Criar um novo objeto
Intl.ListFormat
.
Métodos estáticos
Intl.ListFormat.supportedLocalesOf()
-
Retorna um array com as localidades fornecidas com suporte sem precisar depender da localidade padrão do tempo de execução.
Métodos de instância
Intl.ListFormat.prototype.format()
-
Retorna uma string formatada especifica do idioma representando os elementos da lista.
Intl.ListFormat.prototype.formatToParts()
-
Retorna um array de objetos representando os diferentes componentes que podem ser usados para formatar uma lista de valores com reconhecimento de localidade.
Exemplos
Utilizando format
O exemplo a seguir mostra como criar o List formatter utilizando o idioma Português.
const list = ["Moto", "Ônibus", "Carro"];
console.log(
new Intl.ListFormat("pt-BR", { style: "long", type: "conjunction" }).format(
list,
),
);
// > Moto, Ônibus e Carro
console.log(
new Intl.ListFormat("pt-BR", { style: "short", type: "disjunction" }).format(
list,
),
);
// > Moto, Ônibus ou Carro
console.log(
new Intl.ListFormat("pt-BR", { style: "narrow", type: "unit" }).format(list),
);
// > Moto Ônibus Carro
Utilizando formatToParts
O exemplo a seguir mostra como criar o List formatter retornando as partes formatadas.
const list = ["Moto", "Ônibus", "Carro"];
console.log(
new Intl.ListFormat("pt-BR", {
style: "long",
type: "conjunction",
}).formatToParts(list),
);
// [ { "type": "element", "value": "Moto" },
// { "type": "literal", "value": ", " },
// { "type": "element", "value": "Ônibus" },
// { "type": "literal", "value": ", e " },
// { "type": "element", "value": "Carro" } ];
Especificações
Specification |
---|
ECMAScript® 2025 Internationalization API Specification # listformat-objects |
Compatibilidade de browser
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ListFormat | ||||||||||||||
ListFormat() constructor | ||||||||||||||
format | ||||||||||||||
formatToParts | ||||||||||||||
resolvedOptions | ||||||||||||||
supportedLocalesOf |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- See implementation notes.
- Has more compatibility info.