This commit is contained in:
parent
5452506787
commit
549a7d9e93
@ -2,6 +2,21 @@ import i18n from 'i18next';
|
|||||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||||
import { initReactI18next } from 'react-i18next';
|
import { initReactI18next } from 'react-i18next';
|
||||||
import { Platform } from 'react-native';
|
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';
|
import translations from './translations-generated';
|
||||||
|
|
||||||
@ -29,30 +44,11 @@ i18n
|
|||||||
escapeValue: false, // 不需要转义,React已经处理了
|
escapeValue: false, // 不需要转义,React已经处理了
|
||||||
},
|
},
|
||||||
|
|
||||||
detection: Platform.OS === 'web' ? {
|
detection: {
|
||||||
// Web 环境使用 localStorage
|
order: Platform.OS === 'web' ? ['localStorage', 'navigator'] : ['customStorage', 'device'],
|
||||||
order: ['localStorage', 'navigator'],
|
caches: Platform.OS === 'web' ? ['localStorage'] : ['customStorage'],
|
||||||
caches: ['localStorage'],
|
|
||||||
lookupLocalStorage: 'i18nextLng',
|
lookupLocalStorage: 'i18nextLng',
|
||||||
lookupFromPathIndex: 0,
|
...(customStorage && { lookupCustomStorage: customStorage }),
|
||||||
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) // 使用类型断言绕过类型检查
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user