是否可以对 redux Actions 使用 i18n 本地化?

Vla*_*lad 6 internationalization i18next reactjs redux react-i18next

我试图为 Redux Action 函数实现 i18n 本地化,但我不断收到不同的钩子错误。当我以这种方式实现 i18n 时,我收到错误。

第 428:17 行:在函数“sendDataRequest”中调用 React Hook“useTranslation”,该函数既不是 React 函数组件,也不是自定义 React Hook 函数。

import { useTranslation } from "react-i18next";

export const sendDataRequest = (requestId) => {
  const { t } = useTranslation();
  return async (dispatch) => {
    try {
      dispatch(sendDataRequest());
      await dataAPI.sendDataRequest({ requestId });
      notification.success({
        message: t('infoPage.requestSentSuccessfully'),      
     });
      dispatch(sendDataSuccess());
    } catch (error) {
      dispatch(sendDataFailure());
      console.error(error);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我移动了const { t } = useTranslation(); return 语句内部。但后来我收到另一个错误在此输入图像描述 看来我在这里显然用错了。但我找不到任何有关在 Actions 中使用 i18n 的示例。有谁知道是否可以在 Actions 中使用 i18b?

tim*_*tgl 5

查看https://github.com/i18next/react-i18next/issues/909

您需要获得对您的i18next实例的访问权限。您在代码库中的某个位置创建的。有了这个实例,您只需调用即可i18n.t("my.translation.key")进行翻译。这完全独立于反应。