-
Notifications
You must be signed in to change notification settings - Fork 1
/
i18n.tsx
33 lines (30 loc) · 1.02 KB
/
i18n.tsx
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
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import * as Localization from "expo-localization";
// Here we import the bundle file as defined above
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as resources from "./translation.json";
export const LANG_KEY = "@preferences/lang";
export const initiateTranslation = () => {
return i18n
.use(initReactI18next)
.use({
type: "languageDetector",
async: true,
detect: async (callback: (lng: string) => void) => {
const preferredLang = await AsyncStorage.getItem(LANG_KEY);
if (preferredLang) return callback(preferredLang);
const initialLang = Localization.locale.split("-")[0];
return callback(initialLang);
},
init: () => null,
cacheUserLanguage: async (lng: string) => {
await AsyncStorage.setItem(LANG_KEY, lng);
},
}) // detects default language
.init({
resources,
fallbackLng: "en",
});
};
export default i18n;