【发布时间】:2020-06-28 02:17:28
【问题描述】:
我正在使用 React、i18next 和 i18next-localstorage-cache 开发一个多语言应用程序。 它应该与您刷新页面后选择的语言相同。 我很努力但是.... 但它是默认语言。 我想知道在发布新版本翻译时如何使用户的本地存储缓存无效。我该怎么做?
import i18n from "i18next";
import { reactI18nextModule } from "react-i18next";
import detector from "i18next-browser-languagedetector";
import Cache from 'i18next-localstorage-cache';
import translationEN from '../src/locales/en/translation.json';
import translationRU from '../src/locales/ru/translation.json';
import translationUK from '../src/locales/uk/translation.json';
// const languagedetector = new languagedetector(null, options);
// languagedetector.addDetector(myDectector)
const resources = {
en: {
translation: translationEN
},
ru: {
translation: translationRU
},
uk: {
translation: translationUK
}
}
i18n
.use(detector)
.use(reactI18nextModule)
.use(Cache)
.init({
resources,
lng: "en",
fallbackLng: 'en',
keySeparator: false,
interpolation: {
escapeValue: false
},
Cache: {
enabled: false,
prefix: 'translation_',
expirationTime: Infinity,
Version: {},
// defaultVersion: '',
},
// detector: {
// order: ['querystring', 'cookie', 'localstorage', 'navigator', 'htmlTag', 'path', ' subdomain'],
// lookupQuerystring: 'lng',
// lookupCookie: 'i18next',
// lookupLocalStorage: 'i18nextLng',
// lookupFromPthIndex: 0,
// lookupFromSubdomainIndex: 0,
// cache: ['localStorage', 'cookie'],
// excludeCacheFor: ['cimode'],
// cookieMinutes: 10,
// cookieDomain: 'myDOmain',
// htmlTag: document.documentElement,
// checkWhitelist: true
// }
});
export default i18n
【问题讨论】:
-
本地存储为存储键值对提供了客户端解决方案。看看这里:developer.mozilla.org/de/docs/Web/API/Window/localStorage 用于存储所选语言:
window.localStorage.setItem('language', 'en');用于检索语言:window.localStorage.getItem('language'); -
我看到您在代码中提到了
version,但没有使用它。好像这是你需要的。 github.com/i18next/i18next-localStorage-cache#cache-options
标签: reactjs