我正在使用 React 和react-i18next
我的 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