2019-12-07 16:58:52 +05:30
|
|
|
import { IntlShape, createIntl, createIntlCache } from 'react-intl';
|
2019-12-08 00:32:00 +05:30
|
|
|
import captcha from 'app/services/captcha';
|
|
|
|
import locales from 'app/i18n';
|
2019-11-11 14:10:05 +05:30
|
|
|
|
|
|
|
import intlPolyfill from './intlPolyfill';
|
|
|
|
|
2020-01-20 13:21:37 +05:30
|
|
|
export const SUPPORTED_LANGUAGES = Object.keys(locales);
|
|
|
|
export const DEFAULT_LANGUAGE = 'en';
|
2019-11-11 14:10:05 +05:30
|
|
|
|
|
|
|
function getBrowserPreferredLanguages(): string[] {
|
2020-05-24 04:38:24 +05:30
|
|
|
return ([] as string[]).concat(navigator['languages'] || []).concat(navigator['language'] || []);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
function detectLanguage(userLanguages: string[], availableLanguages: string[], defaultLanguage: string): string {
|
|
|
|
return (
|
|
|
|
userLanguages
|
|
|
|
.map((lang) => (lang.split('-').shift() || '').toLowerCase())
|
|
|
|
.find((lang) => availableLanguages.indexOf(lang) !== -1) || defaultLanguage
|
|
|
|
);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const cache = createIntlCache();
|
|
|
|
|
|
|
|
let intl: IntlShape;
|
|
|
|
|
|
|
|
class I18N {
|
2020-05-24 04:38:24 +05:30
|
|
|
detectLanguage(lang: string = ''): string {
|
|
|
|
return detectLanguage(
|
|
|
|
[lang].concat(getBrowserPreferredLanguages()).filter((item) => !!item),
|
|
|
|
SUPPORTED_LANGUAGES,
|
|
|
|
DEFAULT_LANGUAGE,
|
|
|
|
);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
getIntl(): IntlShape {
|
|
|
|
if (!intl) {
|
|
|
|
intl = createIntl(
|
|
|
|
{
|
|
|
|
locale: 'en',
|
|
|
|
messages: {},
|
|
|
|
},
|
|
|
|
cache,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return intl;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
async changeLocale(locale: string = DEFAULT_LANGUAGE): Promise<IntlShape> {
|
|
|
|
const { messages } = await this.require(locale);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
captcha.setLang(locale);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
intl = createIntl(
|
|
|
|
{
|
|
|
|
locale,
|
|
|
|
messages,
|
|
|
|
},
|
|
|
|
cache,
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return intl;
|
|
|
|
}
|
|
|
|
|
|
|
|
async ensureIntl() {
|
|
|
|
await intlPolyfill('en');
|
|
|
|
}
|
|
|
|
|
2021-07-14 02:10:31 +05:30
|
|
|
async require(locale: string): Promise<{
|
2020-05-24 04:38:24 +05:30
|
|
|
locale: string;
|
2020-06-05 20:54:41 +05:30
|
|
|
messages: Record<string, string>;
|
2020-05-24 04:38:24 +05:30
|
|
|
}> {
|
2020-06-09 23:32:03 +05:30
|
|
|
try {
|
|
|
|
const [{ default: messages }] = await Promise.all([
|
|
|
|
import(/* webpackChunkName: "locale-[request]" */ `app/i18n/${locale}.json`),
|
|
|
|
intlPolyfill(locale),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
locale,
|
|
|
|
messages,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
if (err.message === "Cannot find module './en.json'") {
|
|
|
|
console.warn(
|
|
|
|
[
|
|
|
|
"Locales module for the source language isn't exists.",
|
|
|
|
'You may generate this file by running yarn i18n:extract command.',
|
|
|
|
'Until then, defaultMessages will be used for displaying on the site.',
|
|
|
|
].join(' '),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { locale, messages: {} };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
2020-05-24 04:38:24 +05:30
|
|
|
}
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default new I18N();
|