-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathi18next.js
41 lines (37 loc) · 992 Bytes
/
i18next.js
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
import React from 'react';
import AsyncStorage from '@react-native-community/async-storage';
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import { en, fr } from './localize';
const LOCALE_PERSISTENCE_KEY = 'language';
const languageDetector = {
type: 'languageDetector',
async: true,
detect: async (language) => {
const persistedLocale = await AsyncStorage.getItem(LOCALE_PERSISTENCE_KEY);
if (!persistedLocale) {
// Find best available language from the resource ones
// Return detected locale or default language
return language('en');
}
return language(persistedLocale);
},
init: () => {},
cacheUserLanguage: (locale) => {
AsyncStorage.setItem(LOCALE_PERSISTENCE_KEY, locale);
},
};
i18next
.use(languageDetector)
.use(initReactI18next)
.init({
resources: {
en: {
translation: en,
},
fr: {
translation: fr,
},
},
});
export default i18next;