From 6ccb162a440d498c8217b54014c0dfd222d4d4c0 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 17 Jul 2024 04:20:50 -0400 Subject: [PATCH] Remove dynamic displaynames polyfill loading (#930) --- utils/i18nCountries.js | 78 ++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/utils/i18nCountries.js b/utils/i18nCountries.js index 01373740..edc8c280 100644 --- a/utils/i18nCountries.js +++ b/utils/i18nCountries.js @@ -1,42 +1,52 @@ -import { countryList } from 'country-util' -import '@formatjs/intl-displaynames/polyfill' +import "@formatjs/intl-displaynames/polyfill" -const getLocale = (locale) => { - if (locale === 'zh-CN') return 'zh-Hans' - if (locale === 'pt-BR') return 'pt' - return locale -} -// eventually we can remove this, but currently Chrome doesn't have the translations for UN M.49 area codes implemented so we need to polyfill -process.env.LOCALES.forEach((locale) => { - locale = getLocale(locale) +import "@formatjs/intl-displaynames/locale-data/ar" +import "@formatjs/intl-displaynames/locale-data/de" +import "@formatjs/intl-displaynames/locale-data/en" +import "@formatjs/intl-displaynames/locale-data/es" +import "@formatjs/intl-displaynames/locale-data/fa" +import "@formatjs/intl-displaynames/locale-data/fr" +import "@formatjs/intl-displaynames/locale-data/my" +import "@formatjs/intl-displaynames/locale-data/pt" +import "@formatjs/intl-displaynames/locale-data/ru" +import "@formatjs/intl-displaynames/locale-data/sw" +import "@formatjs/intl-displaynames/locale-data/th" +import "@formatjs/intl-displaynames/locale-data/tr" +import "@formatjs/intl-displaynames/locale-data/vi" +import "@formatjs/intl-displaynames/locale-data/zh-Hans" +import "@formatjs/intl-displaynames/locale-data/zh-Hant" + +import { countryList } from "country-util" - require(`@formatjs/intl-displaynames/locale-data/${locale}`) -}) +const getLocale = (locale) => { + if (locale === "zh-CN") return "zh-Hans" + if (locale === "pt-BR") return "pt" + return locale; +}; export const getLocalisedRegionName = (regionCode, locale) => { - locale = getLocale(locale) - try { - return new Intl.DisplayNames([locale], { type: 'region' }).of(String(regionCode)) - } catch (e) { - return regionCode - } -} + try { + return new Intl.DisplayNames([getLocale(locale)], { type: "region" }).of( + String(regionCode), + ); + } catch (e) { + return regionCode + } +}; export const getLocalisedLanguageName = (regionCode, locale) => { - locale = getLocale(locale) - - try { - return new Intl.DisplayNames([locale], { type: 'language' }).of( - String(regionCode) - ) - } catch (e) { - return regionCode - } -} + try { + return new Intl.DisplayNames([getLocale(locale)], { type: "language" }).of( + String(regionCode), + ); + } catch (e) { + return regionCode + } +}; export const localisedCountries = (locale) => { - return countryList.map((c) => ({ - ...c, - localisedCountryName: getLocalisedRegionName(c.iso3166_alpha2, locale), - })) -} + return countryList.map((c) => ({ + ...c, + localisedCountryName: getLocalisedRegionName(c.iso3166_alpha2, locale), + })); +};