diff --git a/packages/app/components/i18n/index.ts b/packages/app/components/i18n/index.ts index cd42dfd..6b3f992 100644 --- a/packages/app/components/i18n/index.ts +++ b/packages/app/components/i18n/index.ts @@ -1,2 +1,2 @@ export { default as IntlProvider } from './IntlProvider'; -export { default as localeFlags } from './localeFlags'; +export * from './localeFlags'; diff --git a/packages/app/components/i18n/localeFlags.ts b/packages/app/components/i18n/localeFlags.ts index dd233d6..c9d4d96 100644 --- a/packages/app/components/i18n/localeFlags.ts +++ b/packages/app/components/i18n/localeFlags.ts @@ -11,42 +11,36 @@ const localeToCountryCode: Record = { zh: 'cn', cs: 'cz', }; -const SUPPORTED_LANGUAGES: string[] = Object.keys(supportedLocales); +const SUPPORTED_LANGUAGES: ReadonlyArray = Object.keys(supportedLocales); -export default { - getCountryList(): string[] { - return SUPPORTED_LANGUAGES.map((locale) => localeToCountryCode[locale] || locale); - }, +export function getCountriesList(): string[] { + return SUPPORTED_LANGUAGES.map((locale) => localeToCountryCode[locale] || locale); +} - /** - * Возвращает для указанной локали её флаг с учётом всех нюансов загрузки флага - * и подбора соответствующего локали флага. - * - * @param {string} locale - * - * @returns {string} - */ - getIconUrl(locale: string): string { - let mod; +const flagIconLoadingChain: ReadonlyArray<(locale: string) => string | { default: string }> = [ + (locale) => require(`./flags/${locale}.svg`), + (locale) => require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`), + () => require('./flags/unknown.svg'), +]; +/** + * Возвращает для указанной локали её флаг с учётом всех нюансов загрузки флага + * и подбора соответствующего локали флага. + * + * @param {string} locale + * + * @returns {string} + */ +export function getLocaleIconUrl(locale: string): string { + for (const flagIconLoadingChainElement of flagIconLoadingChain) { try { - mod = require(`./flags/${locale}.svg`); - } catch (err1) { - if (!err1.message.startsWith('Cannot find module')) { - throw err1; - } + const mod = flagIconLoadingChainElement(locale); - try { - mod = require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`); - } catch (err2) { - if (!err2.message.startsWith('Cannot find module')) { - throw err2; - } - - mod = require('./flags/unknown.svg'); + return mod.default || mod; + } catch (err) { + if (!err.message.startsWith('Cannot find module')) { + throw err; } } - - return mod.default || mod; - }, -}; + } +} diff --git a/packages/app/components/languageSwitcher/LocaleItem.tsx b/packages/app/components/languageSwitcher/LocaleItem.tsx index 2a39d17..495649e 100644 --- a/packages/app/components/languageSwitcher/LocaleItem.tsx +++ b/packages/app/components/languageSwitcher/LocaleItem.tsx @@ -1,5 +1,5 @@ import React, { ComponentType, ReactNode } from 'react'; -import { localeFlags } from 'app/components/i18n'; +import { getLocaleIconUrl } from 'app/components/i18n'; import { FormattedMessage as Message } from 'react-intl'; import styles from './languageSwitcher.scss'; @@ -31,7 +31,7 @@ const LocaleItem: ComponentType = ({ locale: { code, name, englishName, p
diff --git a/packages/app/components/languageSwitcher/changeLanguageLink/ChangeLanguageLink.tsx b/packages/app/components/languageSwitcher/changeLanguageLink/ChangeLanguageLink.tsx index 55294ca..7f48d2a 100644 --- a/packages/app/components/languageSwitcher/changeLanguageLink/ChangeLanguageLink.tsx +++ b/packages/app/components/languageSwitcher/changeLanguageLink/ChangeLanguageLink.tsx @@ -1,7 +1,7 @@ import React, { ComponentType, useCallback } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import clsx from 'clsx'; -import { localeFlags } from 'app/components/i18n'; +import { getLocaleIconUrl } from 'app/components/i18n'; import LANGS from 'app/i18n'; import { create as createPopup } from 'app/components/ui/popup/actions'; import LanguageSwitcher from 'app/components/languageSwitcher'; @@ -30,7 +30,7 @@ const LanguageLink: ComponentType = () => { {localeDefinition.name} diff --git a/webpack.config.js b/webpack.config.js index 21d9898..dbe6d44 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -17,7 +17,7 @@ const EagerImportsPlugin = require('eager-imports-webpack-plugin').default; const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); const config = require('./config'); const SUPPORTED_LANGUAGES = Object.keys(require('app/i18n').default); -const localeFlags = require('app/components/i18n/localeFlags').default; +const { getCountriesList } = require('app/components/i18n/localeFlags'); const rootPath = path.resolve('./packages'); const outputPath = path.join(__dirname, 'build'); @@ -112,12 +112,12 @@ const webpackConfig = { // @see components/i18n/localeFlags.js new webpack.ContextReplacementPlugin( /flag-icon-css\/flags\/4x3/, - new RegExp(`/(${localeFlags.getCountryList().join('|')})\\.svg`), + new RegExp(`/(${getCountriesList().join('|')})\\.svg`), ), // @see components/i18n/localeFlags.js new webpack.ContextReplacementPlugin( /app\/components\/i18n\/flags/, - new RegExp(`/(${localeFlags.getCountryList().join('|')})\\.svg`), + new RegExp(`/(${getCountriesList().join('|')})\\.svg`), ), ],