mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
#379: move flags related code into a separate module to get rid of code duplication
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import IntlProvider from './IntlProvider';
|
||||
|
||||
export {
|
||||
IntlProvider
|
||||
};
|
||||
// @flow
|
||||
export { default as IntlProvider } from './IntlProvider';
|
||||
export { default as localeFlags } from './localeFlags';
|
||||
|
30
src/components/i18n/localeFlags.js
Normal file
30
src/components/i18n/localeFlags.js
Normal file
@@ -0,0 +1,30 @@
|
||||
// @flow
|
||||
import supportedLocales from 'i18n/index.json';
|
||||
|
||||
const localeToCountryCode = {
|
||||
en: 'gb',
|
||||
be: 'by',
|
||||
pt: 'br',
|
||||
uk: 'ua',
|
||||
vi: 'vn',
|
||||
sl: 'si',
|
||||
};
|
||||
const SUPPORTED_LANGUAGES: Array<string> = Object.keys(supportedLocales);
|
||||
|
||||
export default {
|
||||
getCountryList(): Array<string> {
|
||||
return SUPPORTED_LANGUAGES.map((locale) => localeToCountryCode[locale] || locale);
|
||||
},
|
||||
|
||||
/**
|
||||
* Возвращает для указанной локали её флаг с учётом всех нюансов загрузки флага
|
||||
* и подбора соответствующего локали флага.
|
||||
*
|
||||
* @param {string} locale
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
getIconUrl(locale: string): string {
|
||||
return require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`);
|
||||
}
|
||||
};
|
@@ -5,7 +5,7 @@ import { FormattedMessage as Message, intlShape } from 'react-intl';
|
||||
|
||||
import classNames from 'classnames';
|
||||
|
||||
import { requireLocaleFlag } from 'functions';
|
||||
import { localeFlags } from 'components/i18n';
|
||||
import LANGS from 'i18n/index.json';
|
||||
|
||||
import formStyles from 'components/ui/form/form.scss';
|
||||
@@ -197,7 +197,7 @@ class LanguageSwitcher extends Component {
|
||||
return (
|
||||
<div className={styles.languageFlex}>
|
||||
<div className={styles.languageIco} style={{
|
||||
backgroundImage: `url('${requireLocaleFlag(locale)}')`,
|
||||
backgroundImage: `url('${localeFlags.getIconUrl(locale)}')`,
|
||||
}} />
|
||||
<div className={styles.languageCaptions}>
|
||||
<div className={styles.languageName}>
|
||||
|
@@ -5,7 +5,7 @@ import { FormattedMessage as Message, FormattedRelative as Relative } from 'reac
|
||||
import { Link } from 'react-router-dom';
|
||||
import Helmet from 'react-helmet';
|
||||
|
||||
import { requireLocaleFlag } from 'functions';
|
||||
import { localeFlags } from 'components/i18n';
|
||||
import LANGS from 'i18n/index.json';
|
||||
|
||||
import { userShape } from 'components/user/User';
|
||||
@@ -93,7 +93,7 @@ class Profile extends Component {
|
||||
value={
|
||||
<span className={styles.language} onClick={this.onLanguageSwitcher.bind(this)}>
|
||||
<span className={styles.languageIcon} style={{
|
||||
backgroundImage: `url('${requireLocaleFlag(user.lang)}')`
|
||||
backgroundImage: `url('${localeFlags.getIconUrl(user.lang)}')`
|
||||
}} />
|
||||
{LANGS[user.lang].name}
|
||||
</span>
|
||||
|
@@ -79,23 +79,3 @@ export function getJwtPayload(jwt: string): Object {
|
||||
throw new Error('Can not decode jwt token');
|
||||
}
|
||||
}
|
||||
|
||||
const localeToCountryCode = {
|
||||
en: 'gb',
|
||||
be: 'by',
|
||||
pt: 'br',
|
||||
uk: 'ua',
|
||||
vi: 'vn',
|
||||
sl: 'si',
|
||||
};
|
||||
|
||||
/**
|
||||
* Возвращает для указанной локали её флаг с учётом всех нюансов загрузки флага
|
||||
* и подбора соответствующего локали флага.
|
||||
*
|
||||
* @param {string} locale
|
||||
* @return {*}
|
||||
*/
|
||||
export function requireLocaleFlag(locale: string) {
|
||||
return require(`flag-icon-css/flags/4x3/${localeToCountryCode[locale] || locale}.svg`);
|
||||
}
|
||||
|
Reference in New Issue
Block a user