Skip to content

Commit

Permalink
Remove dynamic displaynames polyfill loading (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel authored Jul 17, 2024
1 parent 9f63285 commit 6ccb162
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions utils/i18nCountries.js
Original file line number Diff line number Diff line change
@@ -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),
}));
};

0 comments on commit 6ccb162

Please sign in to comment.