【问题标题】:Handling locale change with react-native-i18next/locize/locize backend使用 react-native-i18next/locize/locize 后端处理语言环境更改
【发布时间】:2018-09-03 11:49:36
【问题描述】:

我正在尝试获取 RN i18next 并 locize 以根据 react-native-i18n 的当前语言环境提供翻译(已经在本地设置了翻译)

我在使用此处找到的 languageDetector 库时遇到问题: https://github.com/DylanVann/i18next-react-native-language-detector

它会抛出一个关于找不到对象的参数“replace”的错误。

所以我想我会使用以下代码设置自己的 locize/languageDetector。

但是..当区域设置更改时它似乎没有更新..

我做错了什么?

在 i18next.js 中:

import i18next from "i18next";
import LocizeBackend from "i18next-locize-backend";
import I18n from "../i18n/i18n";

const languageDetector = {
 init: Function.prototype,
 type: "languageDetector",
 async: true, // flags below detection to be async
 detect: lng => I18n.locale,
 cacheUserLanguage: () => {}
};

i18next
  .use(languageDetector)
  .use(LocizeBackend)
  .init({
    fallbackLng: I18n.locale,
    lng: I18n.locale,
    debug: true,
    whitelist: ["en", "fr", "sv", "dev"],
    keySeparator: false,
    ns: ["common"],
    defaultNS: "common",
    saveMissing: false, 

    interpolation: {
      escapeValue: false 
    },
    backend: {
      referenceLng: "en",
    }
  });

【问题讨论】:

    标签: react-native i18next


    【解决方案1】:

    看起来您使用的语言检测 I18n.locale 不是异步的 -> 所以删除 async: true...如果您在 i18next.init 上设置语言,它将不会使用检测器...

    您也可以将此检测器用作示例:https://github.com/dormakaba-digital/digital-reactnative-client/blob/master/src/modules/i18n/i18n.js#L4 -> 使用了 deviceInfo 模块

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      相关资源
      最近更新 更多