This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
i18n.ts
63 lines (52 loc) · 1.5 KB
/
i18n.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import en from './assets/locales/en';
import nl from './assets/locales/nl';
import de from './assets/locales/de';
import bn from './assets/locales/bn';
import {NativeModules} from 'react-native';
function getSystemLocale(): string {
let locale;
// iOS
if (
NativeModules.SettingsManager &&
NativeModules.SettingsManager.settings &&
NativeModules.SettingsManager.settings.AppleLanguages
) {
locale = NativeModules.SettingsManager.settings.AppleLanguages[0];
// Android
} else if (NativeModules.I18nManager) {
locale = NativeModules.I18nManager.localeIdentifier;
}
if (typeof locale === 'undefined') {
console.log('Couldnt get locale');
return 'en';
}
return locale;
}
const resources = {en, nl, de, bn};
function getLangPrefix(lng: string): string {
let prefix = 'en';
if (lng.indexOf('_') > -1) {
prefix = lng.split('_')[0];
} else if (lng.indexOf('-') > -1) {
prefix = lng.split('-')[0];
}
if (!resources.hasOwnProperty(prefix)) {
// reset to English if language is not supported
prefix = 'en';
}
return prefix;
}
console.log('System Locale', getSystemLocale());
console.log('Lang Prefix', getLangPrefix(getSystemLocale()));
i18n
.use(initReactI18next) // passes i18n down to react-i18next
.init({
resources,
lng: getLangPrefix(getSystemLocale()),
interpolation: {
escapeValue: false, // react already safes from xss
},
});
export default i18n;