import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import AsyncStorage from '@react-native-async-storage/async-storage'; import zh from './locales/zh.json'; import en from './locales/en.json'; const LANGUAGE_KEY = '@app_language'; // 语言配置类型 export interface LanguageOption { key: string; label: string; value: string; } // 语言选项 export const LANGUAGES: LanguageOption[] = [ { key: 'zh', label: '中文', value: 'zh', }, { key: 'en', label: 'English', value: 'en', }, ]; // 获取存储的语言 export const getStoredLanguage = async (): Promise => { try { const language = await AsyncStorage.getItem(LANGUAGE_KEY); return language || 'zh'; } catch (error) { return 'zh'; } }; // 保存语言选择 export const saveLanguage = async (language: string): Promise => { try { await AsyncStorage.setItem(LANGUAGE_KEY, language); } catch (error) { console.error('保存语言失败:', error); } }; // 初始化 i18n i18n .use(initReactI18next) .init({ compatibilityJSON: 'v4', resources: { zh: { translation: zh }, en: { translation: en }, }, lng: 'zh', fallbackLng: 'zh', interpolation: { escapeValue: false, }, }); // 从存储中加载语言设置 getStoredLanguage().then((language) => { i18n.changeLanguage(language); }); export default i18n;