使用react-native-i18next / locize / locize后端处理语言环境更改

问题描述 投票:0回答:1

我正在尝试获取RN i18next并定位以提供基于react-native-i18n当前语言环境的翻译(已在本地设置翻译)

我遇到了这里找到的languageDetector库的问题:https://github.com/DylanVann/i18next-react-native-language-detector

如果没有找到对象的“替换”参数,则会抛出错误。

所以我想我会使用以下代码设置我自己的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个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.