What is messageformat?
The messageformat npm package is a tool for handling internationalization (i18n) in JavaScript applications. It allows developers to create localized messages using ICU MessageFormat syntax, which supports pluralization, gender, and other complex message formatting needs.
What are messageformat's main functionalities?
Basic Message Formatting
This feature allows you to create and compile basic messages with placeholders that can be replaced with dynamic values.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('Hello, {name}!');
console.log(message({ name: 'Alice' })); // Output: Hello, Alice!
Pluralization
This feature supports pluralization, allowing you to define different message formats based on the quantity of items.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('{count, plural, one {# item} other {# items}}');
console.log(message({ count: 1 })); // Output: 1 item
console.log(message({ count: 5 })); // Output: 5 items
Gender Formatting
This feature allows you to format messages based on gender, providing different message templates for male, female, and other genders.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('{gender, select, male {He} female {She} other {They}} will respond soon.');
console.log(message({ gender: 'male' })); // Output: He will respond soon.
console.log(message({ gender: 'female' })); // Output: She will respond soon.
Other packages similar to messageformat
i18next
i18next is a popular internationalization framework for JavaScript. It provides a comprehensive solution for handling translations, including support for pluralization, context, and interpolation. Compared to messageformat, i18next offers a more extensive ecosystem with plugins and integrations for various frameworks and libraries.
react-intl
react-intl is a library specifically designed for internationalization in React applications. It uses the ICU MessageFormat syntax similar to messageformat but is tightly integrated with React components. It provides components and hooks for formatting dates, numbers, and messages within React applications.
globalize
Globalize is a library for internationalization and localization in JavaScript. It provides support for formatting dates, numbers, and messages, as well as parsing and validating input. Globalize uses CLDR data for localization and offers a more data-driven approach compared to messageformat.
A Polyfill for Intl.MessageFormat
This library provides a runtime for the ECMA-402 Intl.MessageFormat proposal,
which is built on top of the developing Unicode MessageFormat 2.0 specification, "MF2".
NOTE: This means that the v4 release of the messageformat
package has
an entirely different API compared to its earlier major releases,
which were built on top of ICU MessageFormat, aka "MF1".
For that,
please see @messageformat/core
instead.
Usage
npm install --save-exact messageformat@next
import { MessageFormat } from 'messageformat';
Intl.MessageFormat = MessageFormat;
In addition to supporting MF2 syntax,
compilers and formatting function runtimes are also provided for
ICU MessageFormat and Fluent messages:
API
The API provided by this Intl.MessageFormat polyfill is current as of
2022-07-13.
The static MessageFormat.parseResource()
method is not yet provided,
as the message resource syntax is still under development.
const locale = 'en-US';
const msg = '{Today is {$today :datetime dateStyle=medium}}';
const mf = new Intl.MessageFormat(msg, locale);
mf.resolveMessage({ today: new Date('2022-02-02') }).toString();
For more information on additional types and functions provided by this package,
see the API documentation site.