From 89202e094291f75b414b114ef8bd981010aa76ca Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 29 Mar 2021 03:35:53 +0200 Subject: [PATCH] Fix locales flags overriding --- packages/app/components/i18n/localeFlags.ts | 18 ++++++------------ postinstall.js | 8 ++++++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/app/components/i18n/localeFlags.ts b/packages/app/components/i18n/localeFlags.ts index 68f8f08..a10f136 100644 --- a/packages/app/components/i18n/localeFlags.ts +++ b/packages/app/components/i18n/localeFlags.ts @@ -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; } } diff --git a/postinstall.js b/postinstall.js index 17227c7..aa2c36c 100755 --- a/postinstall.js +++ b/postinstall.js @@ -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)); });