Intl.js Polyfill

1.2.5 · active · verified Tue Apr 21

Intl.js is a robust polyfill for the ECMA-402 ECMAScript Internationalization API, designed to provide comprehensive localization methods, including `Intl.NumberFormat` and `Intl.DateTimeFormat`, across various JavaScript environments. Currently at version 1.2.5, this library addresses the absence of native `Intl` support or incomplete locale data in legacy browsers like older Safari, as well as specific Node.js versions prior to 0.12 or lacking full CLDR data. Its release cadence is event-driven, with major/minor updates coinciding with new editions of the Ecma-402 specification (e.g., v1.2.1 for Ecma-402 2016) and significant CLDR data updates (e.g., v1.1.0 for CLDR 28.0.0). Patch releases are frequent for bug fixes and minor improvements. A key advantage of Intl.js is its ability to seamlessly integrate with public polyfill services like `cdn.polyfill.io`, ensuring that the polyfill code and corresponding locale data are only loaded by browsers that genuinely require them, thereby optimizing application performance and bandwidth usage. For server-side Node.js applications, it offers explicit mechanisms to detect and patch the runtime with the necessary `Intl` constructors and locale information. This package is crucial for developers building internationally-aware applications targeting a wide range of client and server environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to conditionally polyfill the `Intl` API in a Node.js environment, checking for native support and applying the polyfill only when necessary, then showcases basic number and date formatting.

var areIntlLocalesSupported = require('intl-locales-supported');

var localesMyAppSupports = [
    'en-US',
    'fr-FR',
    'de-DE',
    'es-ES'
];

// This example demonstrates how to conditionally polyfill the Intl API
// in Node.js environments, ensuring that the necessary locale data is available.
// It checks if global.Intl exists and if it supports the required locales.
// If not, it loads and applies the intl polyfill.

if (global.Intl) {
    // Determine if the built-in `Intl` has the locale data we need.
    if (!areIntlLocalesSupported(localesMyAppSupports)) {
        // `Intl` exists, but it doesn't have the data we need, so load the
        // polyfill and patch the constructors we need with the polyfill's.
        var IntlPolyfill    = require('intl');
        Intl.NumberFormat   = IntlPolyfill.NumberFormat;
        Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
        console.log('Patched built-in Intl with polyfill data for missing locales.');
    } else {
        console.log('Built-in Intl supports all required locales.');
    }
} else {
    // No `Intl` object globally, so use and load the polyfill entirely.
    global.Intl = require('intl');
    console.log('No native Intl, loaded full polyfill.');
}

// Example usage after polyfilling
const number = 123456.789;
console.log('Formatted number (en-US):', new Intl.NumberFormat('en-US').format(number));
console.log('Formatted number (fr-FR):', new Intl.NumberFormat('fr-FR').format(number));

const date = new Date();
console.log('Formatted date (en-US):', new Intl.DateTimeFormat('en-US').format(date));
console.log('Formatted date (de-DE):', new Intl.DateTimeFormat('de-DE', { dateStyle: 'full', timeStyle: 'short' }).format(date));

view raw JSON →