Source

polyfill/polyfillIntl.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isNullOrUndef_1 = __importDefault(require("../validators/isNullOrUndef"));
/**
 * !! Caution:
 *   This might cause a large bundle size.
 *   The intl.js core itself is large (3xx KB).
 *   Use only when only few languages are supported.
 *
 * Polyfill for:
 *   Intl.NumberFormat,
 *   Intl.DateTimeFormat,
 *   Intl.RelativeTimeFormat
 *
 * Require packages:
 *   intl
 *   relative-time-format
 * @category polyfill
 * @module polyfillIntl
 */
var polyfillIntl = function (intlDataGetters, relativeTimeFormatDataGetters) {
    // global here is window equivalent
    if ((0, isNullOrUndef_1.default)(global.Intl)) {
        // Add Intl.NumberFormat and Intl.DateTimeFormat polyfill
        var IntlPolyfill = require('intl');
        // Add Intl.RelativeTimeFormat polyfill
        var RelativeTimeFormat_1 = require('relative-time-format').default;
        // Import data for Intl.NumberFormat and Intl.DateTimeFormat
        intlDataGetters.forEach(function (getData) { return getData(); });
        // Import data for Intl.RelativeTimeFormat
        relativeTimeFormatDataGetters.forEach(function (getData) {
            var locale = getData();
            RelativeTimeFormat_1.addLocale(locale);
        });
        global.Intl = IntlPolyfill;
        // @ts-expect-error: This is a polyfill
        global.Intl.RelativeTimeFormat = RelativeTimeFormat_1;
    }
    else {
        // Safari NumberFormat does not have formatToParts so need polyfill too
        var isNumberFormatAvailable = global.Intl.NumberFormat
            && new global.Intl.NumberFormat().formatToParts;
        if (!isNumberFormatAvailable || !global.Intl.DateTimeFormat) {
            // Add Intl.NumberFormat and Intl.DateTimeFormat polyfill
            var IntlPolyfill = require('intl');
            // Import data for Intl.NumberFormat and Intl.DateTimeFormat
            intlDataGetters.forEach(function (getData) { return getData(); });
            if (!isNumberFormatAvailable)
                global.Intl.NumberFormat = IntlPolyfill.NumberFormat;
            if (!global.Intl.DateTimeFormat)
                global.Intl.NumberFormat = IntlPolyfill.DateTimeFormat;
        }
        // @ts-expect-error: This is validating
        if (!global.Intl.RelativeTimeFormat) {
            // Add Intl.RelativeTimeFormat polyfill
            var RelativeTimeFormat_2 = require('relative-time-format').default;
            // Import data for Intl.RelativeTimeFormat
            relativeTimeFormatDataGetters.forEach(function (getData) {
                var locale = getData();
                RelativeTimeFormat_2.addLocale(locale);
            });
            // @ts-expect-error: This is a polyfill
            global.Intl.RelativeTimeFormat = RelativeTimeFormat_2;
        }
    }
};
exports.default = polyfillIntl;