From b901ecaccad0b7a4eb91dd6fbddb983bf0e73940 Mon Sep 17 00:00:00 2001 From: Steve Repsher Date: Tue, 28 Nov 2023 14:28:10 -0500 Subject: [PATCH] Add missing Intl polyfills to modern build (#18794) --- src/resources/intl-polyfill.ts | 42 ++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/resources/intl-polyfill.ts b/src/resources/intl-polyfill.ts index 24a3135d21..5dd0bf03eb 100644 --- a/src/resources/intl-polyfill.ts +++ b/src/resources/intl-polyfill.ts @@ -1,9 +1,11 @@ -import { shouldPolyfill as shouldPolyfillDateTime } from "@formatjs/intl-datetimeformat/should-polyfill"; -import { shouldPolyfill as shouldPolyfillDisplayName } from "@formatjs/intl-displaynames/should-polyfill"; -import { shouldPolyfill as shouldPolyfillLocale } from "@formatjs/intl-locale/should-polyfill"; -import { shouldPolyfill as shouldPolyfillPluralRules } from "@formatjs/intl-pluralrules/should-polyfill"; -import { shouldPolyfill as shouldPolyfillRelativeTime } from "@formatjs/intl-relativetimeformat/should-polyfill"; +import { shouldPolyfill as shouldPolyfillDateTimeFormat } from "@formatjs/intl-datetimeformat/should-polyfill"; +import { shouldPolyfill as shouldPolyfillDisplayNames } from "@formatjs/intl-displaynames/should-polyfill"; +import { shouldPolyfill as shouldPolyfillGetCanonicalLocales } from "@formatjs/intl-getcanonicallocales/should-polyfill"; import { shouldPolyfill as shouldPolyfillListFormat } from "@formatjs/intl-listformat/should-polyfill"; +import { shouldPolyfill as shouldPolyfillLocale } from "@formatjs/intl-locale/should-polyfill"; +import { shouldPolyfill as shouldPolyfillNumberFormat } from "@formatjs/intl-numberformat/should-polyfill"; +import { shouldPolyfill as shouldPolyfillPluralRules } from "@formatjs/intl-pluralrules/should-polyfill"; +import { shouldPolyfill as shouldPolyfillRelativeTimeFormat } from "@formatjs/intl-relativetimeformat/should-polyfill"; import { getLocalLanguage } from "../util/common-translation"; import { polyfillLocaleData, @@ -13,29 +15,41 @@ import { const polyfillIntl = async () => { const locale = getLocalLanguage(); const polyfills: Promise[] = []; - + if (shouldPolyfillGetCanonicalLocales()) { + await import("@formatjs/intl-getcanonicallocales/polyfill-force"); + } if (shouldPolyfillLocale()) { await import("@formatjs/intl-locale/polyfill-force"); } - if (shouldPolyfillPluralRules(locale)) { - polyfills.push(import("@formatjs/intl-pluralrules/polyfill-force")); - } - if (shouldPolyfillRelativeTime(locale)) { - polyfills.push(import("@formatjs/intl-relativetimeformat/polyfill-force")); - } - if (shouldPolyfillDateTime(locale)) { + if (shouldPolyfillDateTimeFormat(locale)) { polyfills.push( import("@formatjs/intl-datetimeformat/polyfill-force").then(() => polyfillTimeZoneData() ) ); } - if (shouldPolyfillDisplayName(locale)) { + if (shouldPolyfillDisplayNames(locale)) { polyfills.push(import("@formatjs/intl-displaynames/polyfill-force")); } if (shouldPolyfillListFormat(locale)) { polyfills.push(import("@formatjs/intl-listformat/polyfill-force")); } + if (shouldPolyfillNumberFormat(locale)) { + polyfills.push(import("@formatjs/intl-numberformat/polyfill-force")); + } + if (shouldPolyfillPluralRules(locale)) { + polyfills.push( + import("@formatjs/intl-pluralrules/polyfill-force").then( + // Locale data for plural rules breaks current JSON conversions as it includes functions, + // so only import English to avoid huge bundles + // TODo: Setup JS imports instead of JSON fetches + () => import("@formatjs/intl-pluralrules/locale-data/en") + ) + ); + } + if (shouldPolyfillRelativeTimeFormat(locale)) { + polyfills.push(import("@formatjs/intl-relativetimeformat/polyfill-force")); + } if (polyfills.length === 0) { return; }