如何处理 i18next 新类型 DefaultTFuncReturn?

dar*_*izp 6 i18next reactjs

昨天,i18next引入了一种新的返回类型DefaultTFuncReturn,我正在尝试升级此依赖项,但出现了一大堆错误。这是因为它过去返回一个字符串,现在DefaultTFuncReturn每次我使用useTranslation钩子时它都会返回。例如:

const { t } = useTranslation();
...
t('Test')
Run Code Online (Sandbox Code Playgroud)

现在的返回类型t('Test')DefaultTFuncReturn,但是如果我在函数内部使用变量,就像t('{{var}} something', { var })它返回一个字符串一样,那么它在这种情况下可以工作。我在文档中找不到任何解释这一新更改的内容,所以我想知道您是如何处理这个问题的,因为我知道这个包被很多人使用。

Set*_*eth 4

我遇到了同样的问题,并在此处的官方文档中找到了解决方案。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
});
Run Code Online (Sandbox Code Playgroud)