mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-27 23:40:28 +05:30
Cleanup localeFlags.ts [skip ci]
This commit is contained in:
parent
c1a790f48b
commit
347fd59319
@ -1,2 +1,2 @@
|
||||
export { default as IntlProvider } from './IntlProvider';
|
||||
export { default as localeFlags } from './localeFlags';
|
||||
export * from './localeFlags';
|
||||
|
@ -11,42 +11,36 @@ const localeToCountryCode: Record<string, string> = {
|
||||
zh: 'cn',
|
||||
cs: 'cz',
|
||||
};
|
||||
const SUPPORTED_LANGUAGES: string[] = Object.keys(supportedLocales);
|
||||
const SUPPORTED_LANGUAGES: ReadonlyArray<string> = 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;
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<Props> = ({ locale: { code, name, englishName, p
|
||||
<div
|
||||
className={styles.languageIco}
|
||||
style={{
|
||||
backgroundImage: `url('${localeFlags.getIconUrl(code)}')`,
|
||||
backgroundImage: `url('${getLocaleIconUrl(code)}')`,
|
||||
}}
|
||||
/>
|
||||
<div className={styles.languageCaptions}>
|
||||
|
@ -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 = () => {
|
||||
<span
|
||||
className={styles.languageIcon}
|
||||
style={{
|
||||
backgroundImage: `url('${localeFlags.getIconUrl(localeDefinition.code)}')`,
|
||||
backgroundImage: `url('${getLocaleIconUrl(localeDefinition.code)}')`,
|
||||
}}
|
||||
/>
|
||||
{localeDefinition.name}
|
||||
|
@ -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`),
|
||||
),
|
||||
],
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user