我使用i18next和一个自定义语言检测器来检测异步存储中的语言,但看起来检测方法甚至没有被调用,每次都选择了fallbackLanguage = 'en'。
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import en from './languages/english.json';
import hi from './languages/hindi.json';
import kn from './languages/kannada.json';
import ta from './languages/tamil.json';
import AsyncStorage from '@react-native-async-storage/async-storage';
const resources = {
en: en,
hi: hi,
kn: kn,
ta: ta,
};
let fallbackLanguage = 'en';
const languageDetector = {
type: 'languageDetector',
async: true,
detect: async (callback) =>
try {
const language = await AsyncStorage.getItem('language');
console.log('lllanguage', language);
if (language) {
console.log('Detected language:', language);
callback(language);
} else {
console.log('No language detected, falling back to default language:', fallbackLanguage);
callback(fallbackLanguage);
}
} catch (error) {
console.error('Failed to get language from async storage:', error);
callback(fallbackLanguage);
}
},
fallbackLng: fallbackLanguage,
};
i18n
.use(languageDetector)
.use(initReactI18next)
.init({
compatibilityJSON: 'v3',
lng: fallbackLanguage,
resources,
interpolation: {
escapeValue: false,
},
});
export default i18n;
localstorage有项目“语言”,并设置预期的值,我想每次我打开应用程序,它从异步存储检测语言,并没有失去关闭时的状态.
1条答案
按热度按时间a64a0gku1#
用这个更新你的代码来检查init是否正在打印这将只是一个检查来知道languageDetector是否正在执行