2019-07-16 14:30:29 +00:00
|
|
|
import i18n from 'i18n-js';
|
|
|
|
import { I18nManager } from 'react-native';
|
|
|
|
import * as RNLocalize from 'react-native-localize';
|
2021-04-01 12:38:16 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import 'moment/min/locales';
|
2019-07-16 14:30:29 +00:00
|
|
|
|
2021-04-01 12:38:16 +00:00
|
|
|
import { toMomentLocale } from '../utils/moment';
|
|
|
|
import { isRTL } from './isRTL';
|
|
|
|
|
|
|
|
export { isRTL };
|
2020-11-30 21:47:05 +00:00
|
|
|
|
2020-09-17 11:50:27 +00:00
|
|
|
export const LANGUAGES = [
|
|
|
|
{
|
|
|
|
label: 'English',
|
|
|
|
value: 'en',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/en.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: '简体中文',
|
2020-09-24 14:13:23 +00:00
|
|
|
value: 'zh-CN',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/zh-CN.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: '繁體中文',
|
2020-09-24 14:13:23 +00:00
|
|
|
value: 'zh-TW',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/zh-TW.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Deutsch',
|
|
|
|
value: 'de',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/de.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Español (ES)',
|
2020-09-24 14:13:23 +00:00
|
|
|
value: 'es-ES',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/es-ES.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Français',
|
|
|
|
value: 'fr',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/fr.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Português (BR)',
|
2020-09-24 14:13:23 +00:00
|
|
|
value: 'pt-BR',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/pt-BR.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Português (PT)',
|
2020-09-24 14:13:23 +00:00
|
|
|
value: 'pt-PT',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/pt-PT.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Russian',
|
|
|
|
value: 'ru',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/ru.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Nederlands',
|
|
|
|
value: 'nl',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/nl.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: 'Italiano',
|
|
|
|
value: 'it',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/it.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}, {
|
|
|
|
label: '日本語',
|
|
|
|
value: 'ja',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/ja.json')
|
2020-12-01 17:50:59 +00:00
|
|
|
}, {
|
|
|
|
label: 'العربية',
|
|
|
|
value: 'ar',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/ar.json')
|
2021-01-20 21:07:45 +00:00
|
|
|
}, {
|
|
|
|
label: 'Türkçe',
|
|
|
|
value: 'tr',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/tr.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}
|
|
|
|
];
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2020-09-17 11:50:27 +00:00
|
|
|
const translations = LANGUAGES.reduce((ret, item) => {
|
|
|
|
ret[item.value] = item.file;
|
|
|
|
return ret;
|
|
|
|
}, {});
|
|
|
|
|
2021-04-01 12:38:16 +00:00
|
|
|
export const setLanguage = (l) => {
|
|
|
|
if (!l) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// server uses lowercase pattern (pt-br), but we're forced to use standard pattern (pt-BR)
|
2021-04-15 13:31:22 +00:00
|
|
|
let locale = LANGUAGES.find(ll => ll.value.toLowerCase() === l.toLowerCase())?.value;
|
|
|
|
if (!locale) {
|
|
|
|
locale = 'en';
|
|
|
|
}
|
2021-04-01 12:38:16 +00:00
|
|
|
// don't go forward if it's the same language and default language (en) was setup already
|
|
|
|
if (i18n.locale === locale && i18n.translations?.en) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i18n.locale = locale;
|
2021-04-15 13:31:22 +00:00
|
|
|
i18n.translations = { ...i18n.translations, [locale]: translations[locale]?.() };
|
2021-04-01 12:38:16 +00:00
|
|
|
I18nManager.forceRTL(isRTL(locale));
|
|
|
|
I18nManager.swapLeftAndRightInRTL(isRTL(locale));
|
|
|
|
i18n.isRTL = I18nManager.isRTL;
|
|
|
|
moment.locale(toMomentLocale(locale));
|
|
|
|
};
|
2019-07-16 14:30:29 +00:00
|
|
|
|
|
|
|
const defaultLanguage = { languageTag: 'en', isRTL: false };
|
2021-04-01 12:38:16 +00:00
|
|
|
const availableLanguages = Object.keys(translations);
|
|
|
|
const { languageTag } = RNLocalize.findBestAvailableLanguage(availableLanguages) || defaultLanguage;
|
2019-07-16 14:30:29 +00:00
|
|
|
|
2021-04-01 12:38:16 +00:00
|
|
|
setLanguage(languageTag);
|
|
|
|
i18n.fallbacks = true;
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2019-07-16 14:30:29 +00:00
|
|
|
export default i18n;
|