Implement multilingual support by integrating i18n for dynamic text rendering across the app. Added language selection modal in HomeScreen and updated various components to utilize localized strings. Updated package dependencies for async storage and localization.
This commit is contained in:
29
i18n.ts
Normal file
29
i18n.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { I18n } from 'i18n-js';
|
||||
import * as Localization from 'expo-localization';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
import en from './locales/en.json';
|
||||
import tk from './locales/tk.json';
|
||||
import ru from './locales/ru.json';
|
||||
|
||||
const i18n = new I18n({
|
||||
en,
|
||||
tk,
|
||||
ru,
|
||||
});
|
||||
|
||||
i18n.enableFallback = true;
|
||||
|
||||
// Function to initialize the language
|
||||
export const initializeLanguage = async () => {
|
||||
const savedLanguage = await AsyncStorage.getItem('user-language');
|
||||
if (savedLanguage) {
|
||||
i18n.locale = savedLanguage;
|
||||
} else {
|
||||
// If no language is saved, detect from device and default to Turkmen
|
||||
const userLanguageCode = Localization.getLocales()[0]?.languageCode;
|
||||
i18n.locale = ['en', 'tk', 'ru'].includes(userLanguageCode || '') ? userLanguageCode! : 'tk';
|
||||
}
|
||||
};
|
||||
|
||||
export default i18n;
|
||||
Reference in New Issue
Block a user