首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >本地化文件的缓存问题-i18Next

本地化文件的缓存问题-i18Next
EN

Stack Overflow用户
提问于 2021-11-26 14:05:34
回答 1查看 818关注 0票数 3

我正在使用react和ReactiveI18Next来实现我的应用程序的本地化。问题是在更新本地化文件之后。有时,我的json文件的旧版本会缓存在浏览器中。如果用户清理缓存,就可以解决这个问题,但我不能依靠用户知道如何清除缓存。JSON文件位于公共区域设置下。

我刚刚知道如何禁用i18next translation.json文件中的缓存。

代码语言:javascript
运行
复制
i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: "en",
    debug: true,
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json',
      requestOptions: {
        cache: 'no-store',
      },
    },
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
  });

这不是一个理想的解决办法。更好的解决方案-翻译文件需要在每次构建后重新检索。但是现在没有发生这种情况,这种感觉就是哈希没有添加到翻译文件中,如何防止新构建后的缓存?

EN

回答 1

Stack Overflow用户

发布于 2022-11-02 03:10:09

我在使用i18next-localstorage-backend时遇到了同样的问题,这意味着在浏览器中使用localStorage

我只是在init选项中添加了一个defaultVersion,但是为了每次生成一个新版本,我必须生成一个随机的版本号。

这是我的i18n.ts文件:

代码语言:javascript
运行
复制
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import Backend from "i18next-chained-backend";
import LocalStorageBackend from "i18next-localstorage-backend";

function genRandonNumber(length : number) {
  const chars = '0123456789';
  const charLength = chars.length;
  let result = '';
  for ( var i = 0; i < length; i++ ) {
     result += chars.charAt(Math.floor(Math.random() * charLength));
  }
  return result;
}

const LOCAL_DOMAINS = ["localhost", "127.0.0.1"];
const HASH = genRandonNumber(10);

i18n
  .use(Backend)
  // detect user language
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  .init({
    // default fallback will be french
    fallbackLng: 'fr',
    // default namespace to load
    ns: 'header',
    // load languages ressources with lazy loading and caching client side
    backend: {
      backends: [
        LocalStorageBackend, // primary ressources (cache = window.localStorage)
        resourcesToBackend((language, namespace, callback) => {
          import(`/public/locales/${language}/${namespace}.json`)
            .then((resources) => {
              callback(null, resources)
            })
            .catch((error) => {
              callback(error, null)
            })
        }) // fallback ressources (json)
      ],
      backendOptions: [{
        expirationTime: 24 * 60 * 60 * 1000 * 7, // 7 days
        defaultVersion: "v" + HASH // generate a new version every build to refresh
      }]
    },
    // debug only in dev
    debug: LOCAL_DOMAINS.includes(window.location.hostname),

    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
  });

i18n.on('languageChanged', () => {
  document.documentElement.lang = i18n.language;
});

export default i18n;

我的i18n实例有点复杂,但最终它会:

translation.

  • Load

  • 使用localStorage来减少网络连接,并启用速度更快的--仅对给定页面启用所需的JSON文件,这要感谢每一个新构建的localStorage

如果您使用的是多个后端(JSON文件和localStorage),则必须使用i18next-chained-backend

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70125682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档