【问题标题】:How to load selected language when page refresh页面刷新时如何加载选定的语言
【发布时间】: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 

【问题讨论】:

标签: reactjs


【解决方案1】:

删除

lng: "en"

在 init() 内部,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多