React native 在组件外部使用react-I18中的t()

React native 在组件外部使用react-I18中的t(),react-native,react-component,i18next,react-i18next,React Native,React Component,I18next,React I18next,我在react本机应用程序中使用i18next和react-i18next,更具体地说是输入验证。我试图将t()作为no组件中的参数传递,但收到错误“TypeError:n不是函数。(在'n('errorMessages.emailNoMatch')中,'n'未定义)”。任何建议都是高度赞赏的,因为我对编码相对较新,我已经就这个问题搜索了几天 以下是i18n.js代码: 从“i18next”导入i18next; 从“./translations/de/common.json”导入common_d

我在react本机应用程序中使用i18next和react-i18next,更具体地说是输入验证。我试图将t()作为no组件中的参数传递,但收到错误“TypeError:n不是函数。(在'n('errorMessages.emailNoMatch')中,'n'未定义)”。任何建议都是高度赞赏的,因为我对编码相对较新,我已经就这个问题搜索了几天

以下是i18n.js代码:

从“i18next”导入i18next;
从“./translations/de/common.json”导入common_de;
从“./translations/en/common.json”导入公共_en;
从“i18n js”导入i18n;
从“世博会本地化”导入*作为本地化;
//设置翻译
i18next.init({
插值:{escapeValue:false},//React已经进行了转义
lng:'en',//要使用的语言
资源:{
de:{
common:common_de,//“common”是我们的自定义名称空间
},
嗯:{
普通的:普通的,
},
},
是的,
});
导出默认值i18next;
RegisterValidator.js代码的一部分,我试图将t()作为参数传递,但它没有将t作为switch语句中的参数读取:

从“React”导入React;
从“react-i18next”导入{useTranslation};
//从'react-i18next'导入{withTranslation};
常量RegisterValidator=(类型、值、电子邮件、t)=>{
让验证=真;
让warningMsg=[];
开关(类型){
案例“用户名”:
如果(!value.includes(“”))返回[true',];
否则{
warningMsg=t('errorMessages.username');
返回[错误,警告消息];
}
违约:
返回[已验证];
}
};
导出默认注册表验证器;
下面是App.js中的一部分

从“React”导入React;
导入“./i18n”;
从“i18n js”导入i18n;
从“i18next”导入i18next;
从'react-I18nextProvider'导入{I18nextProvider};
函数App(){
返回(
);
}
导出默认应用程序;
t()外部组件将不起作用,而是将消息对象发送到
RegisterValidator
并使用它。范例-

function Component() {
// t is declared here
const message = {
error: t('errorMessage'),
success: t('successMessage')
}

// and then send this message object to RegisterValidator

RegisterValidator(.., .., .., message);

}

不能直接从初始化i18n的文件导入实例

import i18n from "./i18n";

...


i18n.t("required_field"); //and use it like this
我的i18n初始化文件

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend";

import LanguageDetector from "i18next-browser-languagedetector";
import CustomLanguageDetector from "./CustomLanguageDetector";

const languageDetector = new LanguageDetector();
languageDetector.addDetector(CustomLanguageDetector);

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(languageDetector)
  // load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
  // learn more: https://github.com/i18next/i18next-http-backend
  .use(Backend)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    fallbackLng: "en",
    // debug: true,
    load: "languageOnly",
    backend: {
      loadPath: `/i18n/{{lng}}.json`,
    },
    react: { useSuspense: false },
    whitelist: ["pt", "en"],
    nonExplicitWhitelist: true,
    detection: {
      // order and from where user language should be detected
      order: ["CustomLanguageDetector"],
      lookupLocalStorage: "@AppName:language", //Change your appname here if you're storing the languge in local storage.
      caches: ["localStorage"],
      checkWhitelist: true,
    },
  });

export default i18n;

最终对我起作用的是直接从i18next调用t函数,并将名称空间作为前缀传递给t函数

import i18next from 'i18next;

const RegisterValidator = () => {
   return i18next.t('common: errorMessages.username')
}

哪里是对RegisterValidator的调用?它在RegisterScreen.js中调用,在输入元素中也是EditProfileScreen.js。但是,错误只发生在我发布的RegisterValidator代码内部。t函数在其他任何地方都可以使用,因为我在其他文件中使用了功能组件。谢谢,这至少让我更接近了一步。至少它正在处理t(),但是现在我得到了一个错误“i18next::translator:missingKey en translation errorMessages.username errorMessages.username”,这意味着json文件没有被正确处理,即使在我的所有其他文件上它读取它没有问题。我添加了我的init文件以进行比较,这可能会有帮助。你的翻译文件里有那把钥匙吗?