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.
messageformat
The experience and subtlety of your program's text can be important. Messageformat is a mechanism for handling both pluralization and gender in your applications. It can also lead to much better translations, as it's designed to support all the languages included in the Unicode CLDR.
The ICU has an official guide for the format. Messageformat supports and extends all parts of the standard, with the exception of the deprecated ChoiceFormat.
There is a good slide-deck on Plural and Gender in Translated Messages by Markus Scherer and Mark Davis. But, again, remember that many of these problems apply even if you're only outputting english.
What problems does it solve?
Using messageformat, you can separate your code from your text formatting, while enabling much more humane expressions. In other words, you won't need to see this anymore in your output:
There are 1 results.
There are 2 result(s).
Number of results: 3.
On a more fundamental level, messageformat and its associated tools can help you build an effective workflow for UI texts and translations, keeping message sources in human-friendly formats, compiling them into JavaScript during your build phase, and making them easy to use from your application code.
What does it look like?
With this message:
const msgSrc = `{GENDER, select,
male {He}
female {She}
other {They}
} found {RES, plural,
=0 {no results}
one {1 result}
other {# results}
}.`;
You'll get these results:
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const msg = mf.compile(msgSrc);
msg({ GENDER: 'male', RES: 1 });
msg({ GENDER: 'female', RES: 1 });
msg({ GENDER: 'male', RES: 0 });
msg({ RES: 2 });
Getting Started
To install just the core package, use:
npm install messageformat
This includes the MessageFormat compiler and a runtime accessor class that provides a slightly nicer API for working with larger numbers of messages. Our Format Guide will help with the ICU MessageFormat Syntax, and the Build-time Compilation Guide provides some options for integrating messageformat to be a part of your workflow around UI texts and translations.