accounts-frontend/packages/app/components/i18n/localeFlags.ts
2020-07-20 15:19:15 +03:00

47 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}
}