-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dynamic displaynames polyfill loading (#930)
- Loading branch information
Showing
1 changed file
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
})); | ||
}; |