What is @messageformat/core?
@messageformat/core is a JavaScript library that provides tools for handling message formatting in applications. It supports ICU MessageFormat syntax, which allows for complex message formatting including pluralization, gender, and other locale-specific variations.
What are @messageformat/core's main functionalities?
Basic Message Formatting
This feature allows you to format basic messages with variable interpolation. The example shows how to create a message template and compile it with a variable.
const { MessageFormat } = require('@messageformat/core');
const mf = new MessageFormat('en');
const message = mf.compile('Hello, {name}!');
console.log(message({ name: 'Alice' })); // Output: Hello, Alice!
Pluralization
This feature supports pluralization based on the count of items. The example demonstrates how to handle singular and plural forms of a word based on the count.
const { MessageFormat } = require('@messageformat/core');
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 for gender-based message formatting. The example shows how to format messages differently based on the gender of the subject.
const { MessageFormat } = require('@messageformat/core');
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.
Custom Formatting
This feature allows you to define custom formatting functions. The example demonstrates how to add a custom formatter that converts text to uppercase.
const { MessageFormat } = require('@messageformat/core');
const mf = new MessageFormat('en');
mf.addFormat('uppercase', (v) => v.toUpperCase());
const message = mf.compile('This is {value, uppercase}');
console.log(message({ value: 'custom' })); // Output: This is CUSTOM
Other packages similar to @messageformat/core
intl-messageformat
intl-messageformat is a library for formatting messages based on the ICU MessageFormat standard. It provides similar functionality to @messageformat/core, including support for pluralization, gender, and other locale-specific variations. However, it is part of the larger FormatJS suite, which includes additional tools for internationalization.
i18next
i18next is a comprehensive internationalization framework for JavaScript. It supports message formatting, including pluralization and interpolation, but also offers features like language detection, translation management, and integration with various frameworks. It is more feature-rich compared to @messageformat/core, which focuses specifically on message formatting.
react-intl
react-intl is a library for internationalizing React applications. It provides components and APIs for formatting dates, numbers, and messages, including support for ICU MessageFormat. While it offers similar message formatting capabilities as @messageformat/core, it is specifically designed for use with React.