Add multilanguage support
This commit is contained in:
parent
f8cbcb2908
commit
fc53bb14a0
15 changed files with 489 additions and 92 deletions
54
lib/localization/i18n.tsx
Normal file
54
lib/localization/i18n.tsx
Normal file
|
@ -0,0 +1,54 @@
|
|||
import i18n from 'i18next';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
import { en, nl } from "@/assets/languages";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
const STORE_LANGUAGE_KEY = "settings.lang";
|
||||
|
||||
const languageDetectorPlugin = {
|
||||
type: "languageDetector",
|
||||
async: true,
|
||||
init: () => { },
|
||||
detect: async function (callback: (lang: string) => void) {
|
||||
try {
|
||||
// get stored language from Async storage
|
||||
// put your own language detection logic here
|
||||
await AsyncStorage.getItem(STORE_LANGUAGE_KEY).then((language) => {
|
||||
if (language) {
|
||||
//if language was stored before, use this language in the app
|
||||
return callback(language);
|
||||
} else {
|
||||
//if language was not stored yet, use english
|
||||
return callback("nl");
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error reading language", error);
|
||||
}
|
||||
},
|
||||
cacheUserLanguage: async function (language: string) {
|
||||
try {
|
||||
//save a user's language choice in Async storage
|
||||
await AsyncStorage.setItem(STORE_LANGUAGE_KEY, language);
|
||||
} catch (error) { }
|
||||
},
|
||||
};
|
||||
const resources = {
|
||||
en: {
|
||||
translation: en,
|
||||
},
|
||||
nl: {
|
||||
translation: nl,
|
||||
},
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
i18n.use(initReactI18next).use(languageDetectorPlugin).init({
|
||||
resources,
|
||||
compatibilityJSON: 'v3',
|
||||
fallbackLng: "nl",
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
});
|
||||
export default i18n;
|
Loading…
Add table
Add a link
Reference in a new issue