reactjs 我在react 18中遇到了i18next的问题

rnmwe5a2  于 2022-12-18  发布在  React
关注(0)|答案(2)|浏览(403)

我不能在标签内(也不能在任何html标记内)使用来自i18n的t,我也不知道为什么,如果我在jsx的标记外使用它,它会工作得很好,并从我的json文件中获取翻译。
代码示例:<label htmlFor={"username"}>{t("Username")}</label>
我得到错误消息:类型“DefaultTFuncReturn”不能赋给类型“ReactI18NextChild|类型“object”不能赋给类型“ReactI18NextChild|重复。
有没有人遇到过类似的问题,你知道如何解决它吗?

8yparm6h

8yparm6h1#

我通过将标签内容 Package 在一个片段标签中“解决”了这个问题,如下所示:
<label htmlFor={"username"}><>{t("Username")}</></label>
这就像它应该的那样工作,不会引发错误,但我不确定这个解决方案有多可行,而且还没有弄清楚它到底是为什么发生的

mitkmikd

mitkmikd2#

我在这里也回答了这个问题,但也在这里分享。
我遇到了同样的问题,并在official docs here中找到了修复。i18next的t方法现在可以返回一个需要阻止的null值。找到您的i18n配置,可能称为i18n.ts

// i18n.ts
import 'i18next';

// declare custom type options so the return is always a string.

declare module 'i18next' {
  interface CustomTypeOptions {
    returnNull: false
  }
}

// update the initialization so the behavior matches the type:
i18next.init({
  returnNull: false,
  // ... any other initializations here
});

相关问题