From 17ddce7b0edc626a78ef604a7a3eaa1c93558f8b Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 24 Nov 2024 21:59:32 +0800 Subject: [PATCH] fix: store languageChanged --- src/i18n.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/i18n.js b/src/i18n.js index 5366a6b..9ac973f 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -17,13 +17,22 @@ const resources = { }, }; +const getStoredLanguage = () => { + return localStorage.getItem("language") || "zh-CN"; +}; + i18n.use(initReactI18next).init({ resources, - lng: "zh-CN", // 默认语言 + lng: getStoredLanguage(), // 使用localStorage中存储的语言或默认值 fallbackLng: "en", // 当前语言的翻译没有找到时,使用的备选语言 interpolation: { escapeValue: false, // react已经安全地转义 }, }); +// 添加语言改变时的处理函数 +i18n.on("languageChanged", (lng) => { + localStorage.setItem("language", lng); +}); + export default i18n;