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
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import { toMomentLocale } from './moment';
|
2021-04-01 12:38:16 +00:00
|
|
|
import { isRTL } from './isRTL';
|
2022-04-14 20:30:41 +00:00
|
|
|
import englishJson from './locales/en.json';
|
|
|
|
|
|
|
|
type TTranslatedKeys = keyof typeof englishJson;
|
2021-04-01 12:38:16 +00:00
|
|
|
|
|
|
|
export { isRTL };
|
2020-11-30 21:47:05 +00:00
|
|
|
|
2022-04-14 20:30:41 +00:00
|
|
|
interface ILanguage {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
file: () => any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const LANGUAGES: ILanguage[] = [
|
2020-09-17 11:50:27 +00:00
|
|
|
{
|
|
|
|
label: 'English',
|
|
|
|
value: 'en',
|
2021-04-01 12:38:16 +00:00
|
|
|
file: () => require('./locales/en.json')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-14 15:59:30 +00:00
|
|
|
label: 'العربية',
|
|
|
|
value: 'ar',
|
|
|
|
file: () => require('./locales/ar.json')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
2023-12-12 16:28:03 +00:00
|
|
|
{
|
|
|
|
label: 'বাংলা',
|
|
|
|
value: 'bn',
|
|
|
|
file: () => require('./locales/bn-IN.json')
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
{
|
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')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2023-12-11 21:04:10 +00:00
|
|
|
label: 'Español',
|
|
|
|
value: 'es',
|
|
|
|
file: () => require('./locales/es.json')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
2022-12-14 15:59:30 +00:00
|
|
|
{
|
|
|
|
label: 'Finnish',
|
|
|
|
value: 'fi',
|
|
|
|
file: () => require('./locales/fi.json')
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
{
|
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')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
2023-12-12 16:28:03 +00:00
|
|
|
{
|
|
|
|
label: 'हिन्दी',
|
|
|
|
value: 'hi',
|
|
|
|
file: () => require('./locales/hi-IN.json')
|
|
|
|
},
|
|
|
|
|
2023-12-22 15:41:50 +00:00
|
|
|
{
|
|
|
|
label: 'Hungarian',
|
|
|
|
value: 'hu',
|
|
|
|
file: () => require('./locales/hu.json')
|
|
|
|
},
|
|
|
|
|
2022-12-14 15:59:30 +00:00
|
|
|
{
|
|
|
|
label: 'Italiano',
|
|
|
|
value: 'it',
|
|
|
|
file: () => require('./locales/it.json')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '日本語',
|
|
|
|
value: 'ja',
|
|
|
|
file: () => require('./locales/ja.json')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Nederlands',
|
|
|
|
value: 'nl',
|
|
|
|
file: () => require('./locales/nl.json')
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
{
|
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')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
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')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
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')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-14 15:59:30 +00:00
|
|
|
label: 'Slovenian (Slovenia)',
|
|
|
|
value: 'sl-SI',
|
|
|
|
file: () => require('./locales/sl-SI.json')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-14 15:59:30 +00:00
|
|
|
label: 'Swedish',
|
|
|
|
value: 'sv',
|
|
|
|
file: () => require('./locales/sv.json')
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
2023-12-12 16:28:03 +00:00
|
|
|
{
|
|
|
|
label: 'தமிழ்',
|
|
|
|
value: 'ta',
|
|
|
|
file: () => require('./locales/ta-IN.json')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'తెలుగు',
|
|
|
|
value: 'te',
|
|
|
|
file: () => require('./locales/te-IN.json')
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
{
|
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')
|
2022-11-04 18:13:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-14 15:59:30 +00:00
|
|
|
label: '简体中文',
|
|
|
|
value: 'zh-CN',
|
|
|
|
file: () => require('./locales/zh-CN.json')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '繁體中文',
|
|
|
|
value: 'zh-TW',
|
|
|
|
file: () => require('./locales/zh-TW.json')
|
2020-09-17 11:50:27 +00:00
|
|
|
}
|
|
|
|
];
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2022-04-14 20:30:41 +00:00
|
|
|
interface ITranslations {
|
|
|
|
[language: string]: () => typeof englishJson;
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:50:27 +00:00
|
|
|
const translations = LANGUAGES.reduce((ret, item) => {
|
|
|
|
ret[item.value] = item.file;
|
|
|
|
return ret;
|
2022-04-14 20:30:41 +00:00
|
|
|
}, {} as ITranslations);
|
2020-09-17 11:50:27 +00:00
|
|
|
|
2022-04-14 20:30:41 +00:00
|
|
|
export const setLanguage = (l: string) => {
|
2021-04-01 12:38:16 +00:00
|
|
|
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));
|
2022-04-14 20:30:41 +00:00
|
|
|
// TODO: Review this logic
|
|
|
|
// @ts-ignore
|
2021-04-01 12:38:16 +00:00
|
|
|
i18n.isRTL = I18nManager.isRTL;
|
|
|
|
moment.locale(toMomentLocale(locale));
|
|
|
|
};
|
2019-07-16 14:30:29 +00:00
|
|
|
|
2021-04-27 16:18:15 +00:00
|
|
|
i18n.translations = { en: translations.en?.() };
|
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
|
|
|
|
2022-04-14 20:30:41 +00:00
|
|
|
// @ts-ignore
|
|
|
|
i18n.isTranslated = (text?: string) => text in englishJson;
|
|
|
|
|
2021-04-01 12:38:16 +00:00
|
|
|
setLanguage(languageTag);
|
|
|
|
i18n.fallbacks = true;
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2022-04-14 20:30:41 +00:00
|
|
|
type Ti18n = {
|
|
|
|
isRTL: boolean;
|
|
|
|
t(scope: TTranslatedKeys, options?: any): string;
|
|
|
|
isTranslated: (text?: string) => boolean;
|
|
|
|
} & typeof i18n;
|
|
|
|
|
|
|
|
export default i18n as Ti18n;
|