
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
messageformat
Advanced tools
Intl.MessageFormat / Unicode MessageFormat 2 parser, runtime and polyfill
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.
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.
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 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 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.
This library provides a formatter and other tools for Unicode MessageFormat 2.0 (MF2), the new standard for localization developed by the MessageFormat Working Group.
This includes a formatter that can be used as a polyfill for the proposed ECMA-402 Intl.MessageFormat formatter.
The API provided by this library is current as of the LDML 47 (March 2025) Final version of the MF2 specification.
npm install --save messageformat
import { MessageFormat } from 'messageformat';
const msg = 'Today is {$today :datetime dateStyle=medium}';
const mf = new MessageFormat('en', msg);
mf.format({ today: new Date('2022-02-02') });
// 'Today is Feb 2, 2022'
The library also provides a number of other tools and utilities for MF2, such as:
MF2 data model conversion tools
import { parseMessage, stringifyMessage } from 'messageformat';
MF2 data model validation and transformation tools
import { validate, visit } from 'messageformat';
Concreate Syntax Tree (CST) tools for MF2
import { parseCST, messageFromCST, stringifyCST } from 'messageformat/cst';
Utilities for building custom function handlers for MF2
import {
DraftFunctions,
asPositiveInteger,
asString
} from 'messageformat/functions';
In addition to supporting MF2 syntax, compilers and formatting function runtimes are also provided for ICU MessageFormat and Fluent messages:
For more information on the types and functions provided by this package, see the documentation site.
[!IMPORTANT] 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.
FAQs
Intl.MessageFormat / Unicode MessageFormat 2 parser, runtime and polyfill
The npm package messageformat receives a total of 295,404 weekly downloads. As such, messageformat popularity was classified as popular.
We found that messageformat demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.