What is @messageformat/runtime?
@messageformat/runtime is a runtime library for handling message formatting in JavaScript. It allows for the creation of localized messages using ICU MessageFormat syntax, which supports pluralization, gender, and other complex message formatting needs.
What are @messageformat/runtime'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/runtime');
const mf = new MessageFormat('en');
const msg = mf.compile('Hello, {name}!');
console.log(msg({ name: 'Alice' }));
Pluralization
This feature supports pluralization, allowing you to format messages differently based on the quantity of items.
const { MessageFormat } = require('@messageformat/runtime');
const mf = new MessageFormat('en');
const msg = mf.compile('{count, plural, one {# item} other {# items}}');
console.log(msg({ count: 1 }));
console.log(msg({ count: 5 }));
Gender Formatting
This feature allows for gender-based message formatting, enabling different messages based on the gender of the subject.
const { MessageFormat } = require('@messageformat/runtime');
const mf = new MessageFormat('en');
const msg = mf.compile('{gender, select, male {He} female {She} other {They}} will respond soon.');
console.log(msg({ gender: 'male' }));
console.log(msg({ gender: 'female' }));
Other packages similar to @messageformat/runtime
intl-messageformat
intl-messageformat is a library for formatting ICU MessageFormat strings. It provides similar functionality to @messageformat/runtime, including support for pluralization and gender formatting. However, it is part of the larger FormatJS suite, which offers additional tools for internationalization.
i18next
i18next is a comprehensive internationalization framework for JavaScript. It supports message formatting, including pluralization and gender, but also offers features like language detection, interpolation, and more. It is more feature-rich compared to @messageformat/runtime, but may be overkill for simple message formatting needs.
react-intl
react-intl is a library specifically designed for internationalizing React applications. It provides components and APIs for formatting dates, numbers, and messages, including support for ICU MessageFormat. It is more specialized for React, whereas @messageformat/runtime is a more general-purpose solution.
@messageformat/runtime
A collection of messageformat runtime utility functions.
npm install @messageformat/runtime
This package should be marked as a dependency for any package that publishes the output of compileModule(), as the compiled ES source output may include references to it.
For applications that bundle their output using e.g. Webpack this is not necessary.
The Messages
accessor class is a completely optional addition.
See also @messageformat/react for a React-specific solution.
This package was previously named messageformat-runtime.
Messageformat is an OpenJS Foundation project, and we follow its Code of Conduct.