forked from NextGenXplorer/Reshme_Info
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathi18n.ts
More file actions
54 lines (47 loc) · 1.3 KB
/
i18n.ts
File metadata and controls
54 lines (47 loc) · 1.3 KB
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
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import AsyncStorage from '@react-native-async-storage/async-storage';
import en from './locales/en.json';
import kn from './locales/kn.json';
const LANGUAGE_STORAGE_KEY = '@reshme_language_preference';
// Load saved language preference
const loadLanguage = async () => {
try {
const savedLanguage = await AsyncStorage.getItem(LANGUAGE_STORAGE_KEY);
return savedLanguage || 'en';
} catch (error) {
console.error('Error loading language preference:', error);
return 'en';
}
};
// Initialize i18n
const initI18n = async () => {
const savedLanguage = await loadLanguage();
i18n.use(initReactI18next).init({
resources: {
en: {
translation: en,
},
kn: {
translation: kn,
},
},
lng: savedLanguage,
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
});
};
// Initialize immediately
initI18n();
// Export function to save language preference
export const saveLanguagePreference = async (language: string) => {
try {
await AsyncStorage.setItem(LANGUAGE_STORAGE_KEY, language);
console.log('Language preference saved:', language);
} catch (error) {
console.error('Error saving language preference:', error);
}
};
export default i18n;