fix: store languageChanged

This commit is contained in:
hamster1963 2024-11-24 21:59:32 +08:00
parent a949bf7e03
commit 17ddce7b0e

View File

@ -17,13 +17,22 @@ const resources = {
}, },
}; };
const getStoredLanguage = () => {
return localStorage.getItem("language") || "zh-CN";
};
i18n.use(initReactI18next).init({ i18n.use(initReactI18next).init({
resources, resources,
lng: "zh-CN", // 默认语言 lng: getStoredLanguage(), // 使用localStorage中存储的语言或默认值
fallbackLng: "en", // 当前语言的翻译没有找到时,使用的备选语言 fallbackLng: "en", // 当前语言的翻译没有找到时,使用的备选语言
interpolation: { interpolation: {
escapeValue: false, // react已经安全地转义 escapeValue: false, // react已经安全地转义
}, },
}); });
// 添加语言改变时的处理函数
i18n.on("languageChanged", (lng) => {
localStorage.setItem("language", lng);
});
export default i18n; export default i18n;