mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-03 12:10:45 +05:30
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import supportedLocales from 'app/i18n';
|
||
|
||
const localeToCountryCode: Record<string, string> = {
|
||
en: 'gb',
|
||
be: 'by',
|
||
pt: 'br',
|
||
uk: 'ua',
|
||
vi: 'vn',
|
||
sl: 'si',
|
||
sr: 'rs',
|
||
zh: 'cn',
|
||
cs: 'cz',
|
||
};
|
||
const SUPPORTED_LANGUAGES: ReadonlyArray<string> = Object.keys(supportedLocales);
|
||
|
||
export function getCountriesList(): string[] {
|
||
return SUPPORTED_LANGUAGES.map((locale) => localeToCountryCode[locale] || locale);
|
||
}
|
||
|
||
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 {
|
||
const mod = flagIconLoadingChainElement(locale);
|
||
|
||
return mod.default || mod;
|
||
} catch (err) {
|
||
if (!err.message.startsWith('Cannot find module')) {
|
||
throw err;
|
||
}
|
||
}
|
||
}
|
||
}
|