What is @formatjs/intl-listformat?
The @formatjs/intl-listformat package is a JavaScript library that provides advanced list formatting capabilities. It allows developers to format lists according to the locale-specific conventions. This can include adjusting the list format for different languages and regions, handling conjunctions, disjunctions, and unit lists in a locale-aware manner.
What are @formatjs/intl-listformat's main functionalities?
Locale-aware formatting of conjunctions
This feature allows the formatting of lists using conjunctions (e.g., 'and' in English) according to the specified locale. It is useful for creating human-readable lists in user interfaces.
import { IntlListFormat } from '@formatjs/intl-listformat';
const formatter = new IntlListFormat('en', { style: 'long', type: 'conjunction' });
const formattedList = formatter.format(['Apple', 'Orange', 'Banana']);
console.log(formattedList); // Output: 'Apple, Orange, and Banana'
Locale-aware formatting of disjunctions
This feature supports formatting lists with disjunctions (e.g., 'or' in English), adapting to the locale's specific rules and preferences. It's particularly useful in contexts where choices are presented to users.
import { IntlListFormat } from '@formatjs/intl-listformat';
const formatter = new IntlListFormat('en', { style: 'long', type: 'disjunction' });
const formattedList = formatter.format(['Apple', 'Orange', 'Banana']);
console.log(formattedList); // Output: 'Apple, Orange, or Banana'
Locale-aware formatting of unit lists
This feature is designed for formatting lists of units, making it easier to display measurements and other unit-based data in a way that conforms to local customs.
import { IntlListFormat } from '@formatjs/intl-listformat';
const formatter = new IntlListFormat('en', { style: 'short', type: 'unit' });
const formattedList = formatter.format(['1 lb', '12 oz']);
console.log(formattedList); // Output: '1 lb, 12 oz'
Other packages similar to @formatjs/intl-listformat
intl-list-format
This package also provides functionality for formatting lists based on locale settings. It is similar to @formatjs/intl-listformat but may not have as extensive support for different locales or customization options.
Intl ListFormat
A spec-compliant polyfill/ponyfill for Intl.ListFormat fully tested by the official ECMAScript Conformance test suite
Installation
npm install @formatjs/intl-listformat
Requirements
This package requires the following capabilities:
- Intl.PluralRules
- Object.is
NOTE: formatToParts
only works on Node 10+ due to lack of Intl.NumberFormat.prototype.formatToParts
natively
Usage
To use the polyfill, just import it to make sure that a fully functional Intl.ListFormat is available in your environment:
import '@formatjs/intl-listformat/polyfill'
If Intl.ListFormat already exists, the polyfill will not be loaded.
To use this as a ponyfill:
import IntlListFormat from '@formatjs/intl-listformat'
By default, this library comes with en
data. To load additional locale, you can include them on demand:
import '@formatjs/intl-listformat/polyfill';
import '@formatjs/intl-listformat/dist/locale-data/de';
If you want to polyfill all locales (e.g for Node):
import '@formatjs/intl-listformat/polyfill-locales'
Tests
This library is fully test262-compliant.
License
This software is free to use under the Yahoo! Inc. BSD license.
See the [LICENSE file][license] for license text and copyright information.