【问题标题】:React i18next useTranslation Hook in helper method在辅助方法中反应 i18next useTranslation Hook
【发布时间】:2020-04-11 04:06:30
【问题描述】:

我正在使用带有 react-i18next 的 React

我的 index.tsx 文件包含一些组件,我可以在那里使用翻译功能

index.js
import React, { Suspense } from 'react'
import ReactDOM from 'react-dom';
import * as utils from './Utils';
import './i18n';
import { useTranslation, withTranslation, Trans } from 'react-i18next';

...
  const { t, i18n } = useTranslation();
  //I can use the translate function here
  t("title");
  //call a util function
  utils.helperFunction(...);
...

这里一切正常。 我现在在一个附加文件中创建了一些辅助函数

Utils.tsx
...
import { useTranslation, withTranslation, Trans } from 'react-i18next';
...
export function helperFunction(props: any){
   //do stuff

   //now I need some translation here - but useTranslation is not working?
   const { t, i18n } = useTranslation();
   t("needTranslation");
}

如何在辅助函数中使用相同的翻译逻辑? (并不总是将t 函数传递给辅助方法)

或者这里使用钩子是错误的方法?

出现以下错误

React Hook "useTranslation" is called in function "helperFunction" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks

【问题讨论】:

    标签: javascript reactjs react-i18next


    【解决方案1】:

    我不再使用 useTranslation 挂钩解决了我的问题

    相反,我将 i18n 初始化移动到了一个文件中(i18n.tsx - 导出 i18n) 并在我的 Utils 类中导入和使用它

    我的 Utils.tsx 文件现在看起来像这样

    Utils.tsx

    ...
    import i18n from '../i18n';
    ...
    export function helperFunction(props: any){
       //do stuff
    
       //use imported i18n and call the t() method
       i18n.t("needTranslation");
    }
    

    i18n.tsx

    import i18n from "i18next";
    import Backend from 'i18next-xhr-backend';
    import { initReactI18next } from 'react-i18next';
    
    i18n
      .use(Backend) 
      .use(initReactI18next) // passes i18n down to react-i18next
      .init({
        lng: "es",
        fallbackLng: 'en',
        backend: {
          loadPath: '/static/locales/{{lng}}/{{ns}}.json'
        },    
        interpolation: {
          escapeValue: false
        }
      });
    
      export default i18n;
    

    【讨论】:

    • 这是正确答案。我想提一下,但是当您想使用命名空间时,这不起作用。你需要写 i18n.t(namespace:needTranslation)
    【解决方案2】:

    您似乎忘记了引用 t("needTranslation); -> t("needTranslation");

    运行您的代码后,我明白了为什么您的代码无法正常工作。如果您想将组件逻辑提取到可重用的函数中,那么您应该制作一个自定义挂钩。更多信息请查看docs

    import React from "react";
    import ReactDOM from "react-dom";
    import i18n from "i18next";
    import "./i18n.js";
    import { useTranslation, initReactI18next } from "react-i18next";
    
    i18n
      .use(initReactI18next) 
      .init({
        resources: {
          en: {
            translation: {
              title: "Hello world",
              subtitle: "Hello stackoverflow"
            }
          }
        },
        lng: "en",
        fallbackLng: "en",
    
        interpolation: {
          escapeValue: false
        }
      });
    
    function App() {
      const { t } = useTranslation();
      useHelperFunction();
      return <h1>{t("title")}</h1>;
    }
    
    // you need to turn the helper function into a hook
    function useHelperFunction() {
      const { t } = useTranslation();
      return <h2>{t("subtitle")}</h2>;
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    【讨论】:

    • 你是对的,我为 SO 简化了我的示例并没有检查语法 - 但我的问题与语法无关,我更新了代码并添加了错误消息
    • 感谢您的帮助,但很抱歉它对我不起作用(我的应用程序中出现同样的错误),但我现在使用 i18n 没有钩子并且一切正常(请参阅我的回答)
    猜你喜欢
    • 2019-08-06
    • 2021-10-30
    • 2021-07-02
    • 1970-01-01
    • 2020-07-10
    • 2021-12-29
    • 2021-01-10
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多