This commit is contained in:
parent
5452506787
commit
549a7d9e93
@ -2,6 +2,21 @@ import i18n from 'i18next';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
// Conditionally import expo-secure-store only on native platforms
|
||||
let customStorage: any;
|
||||
if (Platform.OS !== 'web') {
|
||||
try {
|
||||
const SecureStore = require('expo-secure-store');
|
||||
customStorage = {
|
||||
getItem: (key: string) => SecureStore.getItemAsync(key),
|
||||
setItem: (key: string, value: string) => SecureStore.setItemAsync(key, value),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('Failed to load expo-secure-store', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 自动生成的导入
|
||||
import translations from './translations-generated';
|
||||
|
||||
@ -29,30 +44,11 @@ i18n
|
||||
escapeValue: false, // 不需要转义,React已经处理了
|
||||
},
|
||||
|
||||
detection: Platform.OS === 'web' ? {
|
||||
// Web 环境使用 localStorage
|
||||
order: ['localStorage', 'navigator'],
|
||||
caches: ['localStorage'],
|
||||
detection: {
|
||||
order: Platform.OS === 'web' ? ['localStorage', 'navigator'] : ['customStorage', 'device'],
|
||||
caches: Platform.OS === 'web' ? ['localStorage'] : ['customStorage'],
|
||||
lookupLocalStorage: 'i18nextLng',
|
||||
lookupFromPathIndex: 0,
|
||||
lookupFromSubdomainIndex: 0
|
||||
} : {
|
||||
// 原生环境使用自定义存储
|
||||
order: ['customStorage', 'device'],
|
||||
caches: ['customStorage'],
|
||||
// 使用类型断言解决类型问题
|
||||
...({
|
||||
lookupCustomStorage: {
|
||||
getItem: async (key: string) => {
|
||||
const { getItemAsync } = await import('expo-secure-store');
|
||||
return getItemAsync(key);
|
||||
},
|
||||
setItem: async (key: string, value: string) => {
|
||||
const { setItemAsync } = await import('expo-secure-store');
|
||||
await setItemAsync(key, value);
|
||||
}
|
||||
}
|
||||
} as any) // 使用类型断言绕过类型检查
|
||||
...(customStorage && { lookupCustomStorage: customStorage }),
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user