Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease build size by removing dynamic displaynames polyfill loading #930

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
}));
};
Loading