Fix locales flags overriding

This commit is contained in:
ErickSkrauch 2021-03-29 03:35:53 +02:00
parent f62d9f915f
commit 89202e0942
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
2 changed files with 12 additions and 14 deletions

View File

@ -19,11 +19,6 @@ export function getCountriesList(): string[] {
return SUPPORTED_LANGUAGES.map((locale) => localeToCountryCode[locale] || locale);
}
const flagIconLoadingChain: ReadonlyArray<(locale: string) => { default: string }> = [
(locale) => require(`./flags/${locale}.svg`),
(locale) => require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`),
];
/**
* Возвращает для указанной локали её флаг с учётом всех нюансов загрузки флага
* и подбора соответствующего локали флага.
@ -33,13 +28,12 @@ const flagIconLoadingChain: ReadonlyArray<(locale: string) => { default: string
* @returns {string}
*/
export function getLocaleIconUrl(locale: string): string {
for (const flagIconLoadingChainElement of flagIconLoadingChain) {
try {
return flagIconLoadingChainElement(locale).default;
} catch (err) {
if (!err.message.startsWith('Cannot find module')) {
throw err;
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`).default;
} catch (err) {
if (!err.message.startsWith('Cannot find module')) {
throw err;
}
}

View File

@ -5,14 +5,18 @@
const fs = require('fs');
const path = require('path');
const localeOverrides = {
const overrideMap = {
// Override some intl definitions
'packages/app/services/i18n/overrides/intl': 'node_modules/intl/locale-data/jsonp',
'packages/app/services/i18n/overrides/pluralrules': 'node_modules/@formatjs/intl-pluralrules/dist/locale-data',
// eslint-disable-next-line prettier/prettier
'packages/app/services/i18n/overrides/relativetimeformat': 'node_modules/@formatjs/intl-relativetimeformat/dist/locale-data',
// Override country flags
'packages/app/components/i18n/flags': 'node_modules/flag-icon-css/flags/4x3',
};
Object.entries(localeOverrides).forEach(([sourceDir, targetDir]) => {
Object.entries(overrideMap).forEach(([sourceDir, targetDir]) => {
fs.readdirSync(sourceDir).forEach((localeFile) => {
fs.copyFileSync(path.join(sourceDir, localeFile), path.join(targetDir, localeFile));
});