【问题标题】:How to update i18next configuration?如何更新 i18next 配置?
【发布时间】:2020-07-19 22:17:51
【问题描述】:

我想在我的项目中使用 react-i18next,并且我想使用从服务器获取的数据来配置 i18 设置,所以它是一个异步函数。

我想到的第一个 senario 就是使用这个:

  useEffect(() => {
    console.log("i18n_useEffect", i18n);
    setTimeout(function() {
      console.log(
        "fetch data from server,and then reset i18n.js configuration"
      );
      const configurationFromServer = {...};
      localStorage.setItem("i18n_configuration",configurationFromServer)
      seti18nLoaded(true);
    }, 3000);
  },[]);

seti18nLoaded && <Componenrts/>

首先,我们从服务器获取配置数据,完成后,我们可以在里面显示和使用 i18n。

但它看起来不正确,因为如果配置 api 失败或在我得到数据之前。我想先给个默认设置,以后再更新。

像这样:

import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";

const init_i18n = configFromServer => {
  i18n
    .use(LanguageDetector)
    .use(initReactI18next)
    .init(configFromServer);
};

const defaultConfig = {
  // we init with resources
  resources: {},
  fallbackLng: "en",
  debug: true,
  // have a common namespace used around the full app
  ns: ["translations"],
  defaultNS: "translations",

  keySeparator: false, // we use content as keys

  interpolation: {
    escapeValue: false
  }
};

const configFromServer = {
  // we init with resources
  resources: {
    en: {
      translations: {
        Hello: "Hello",
        welcome: "welcome",
        "Use string as key": "Use string as key"
      }
    },
    de: {
      translations: {
        "Use string as key": "Use string as key_de",
        Hello: "Hello_de",
        welcome: "welcome_de"
      }
    }
  },
  fallbackLng: "en",
  debug: true,

  // have a common namespace used around the full app
  ns: ["translations"],
  defaultNS: "translations",

  keySeparator: false, // we use content as keys

  interpolation: {
    escapeValue: false
  }
};

const initialize = () => {
  i18n
    .use(LanguageDetector)
    .use(initReactI18next)
    .init(defaultConfig);

  setTimeout(() => {
    console.log("fetch data from server");
    //I want update the configuration here.but not work.
    init_i18n(configFromServer);
  }, 2000);
};

initialize();

export default i18n;

所以我们的想法是我们使用默认配置来初始化一次,然后根据我们从服务器拉取的数据更新它

我将使用下面的方式,因为我希望它在 i18next 配置从服务器加载或获取失败之前默认显示英文文本。

我不确定我的想法是否是正确的方法。我还看了一下i18next-xhr-backend,它看起来在init函数中配置了api路径,但是很难理解,如果有人可以举个例子来说明i18next-xhr-backend的用法会很有帮助.

无论如何,我这里有一个在线演示,可以更清楚地解释它: https://codesandbox.io/s/react-i18next-gpzyc

我有一个 fack api 来测试配置: https://my-json-server.typicode.com/jjzjx118/demo/db

它返回:

{
  "en": {
    "translations": {
      "Hello": "Hello",
      "welcome": "welcome",
      "Use string as key": "Use string as key"
    }
  },
  "de": {
    "translations": {
      "Use string as key": "Use string as key_de",
      "Hello": "Hello_de",
      "welcome": "welcome_de"
    }
  }
}

谢谢你。

【问题讨论】:

    标签: javascript reactjs i18next react-i18next


    【解决方案1】:

    我检查了您的沙盒链接,i18n 实例已从新配置中重新初始化。您缺少的是手动选择您喜欢的语言。

    由于您使用LanguageDetector,它只会根据浏览器等选择语言。如果您想在从服务器获取请求后手动更改它,您可以在重新初始化后调用i18n.changeLanguage("de") .

    在你的情况下:

    setTimeout(() => {
        console.log("fetch data from server");
        init_i18n(configFromServer);
    
        // You can change the language here based on the config
        i18n.changeLanguage("de");
      }, 2000);
    

    这是一个工作沙箱https://codesandbox.io/s/react-i18next-we8xi

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2022-12-15
      • 1970-01-01
      相关资源
      最近更新 更多