首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:您正在传递一个未定义的模块!请检查传递给i18next.use()的对象

错误:您正在传递一个未定义的模块!请检查传递给i18next.use()的对象
EN

Stack Overflow用户
提问于 2022-03-18 20:02:39
回答 1查看 1.5K关注 0票数 2

由于上述问题,我的单元测试失败了。

代码语言:javascript
复制
//String.ts
import * as i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import BrowserLanguageDetector from 'i18next-browser-languagedetector'
import Backend from 'i18next-http-backend'
import languageMap from '@katal/localization/webpack-loader!'

i18n
  .use(initReactI18next)
  .use(BrowserLanguageDetector)
  .use(Backend)
  .init({
    fallbackLng: 'en-US',
    load: 'currentOnly',
    detection: {
      order: ['sessionStorage', 'localStorage', 'querystring', 'navigator'],
      lookupQuerystring: 'locale',
      lookupLocalStorage: 'locale',
      lookupSessionStorage: 'locale',
      caches: [],
    },
    backend: {
      loadPath: (localeList: string[]) => languageMap[localeList[0]],
    },
    interpolation: {
      escapeValue: false,
    },
    react: {
      useSuspense: false,
    },
  })

我嘲笑的方式是:

代码语言:javascript
复制
const React = require('react')
const reactI18next = require('react-i18next')



module.exports = {
  ...reactI18next,
  // this mock makes sure any components using the translate HoC or useTranslation hook receive the t function as a prop
  useTranslation: (...args) => ({
    ...reactI18next.useTranslation(...args),
    ready: true,
    t: (tr) => tr,
  }),
}

我把这个导入到我的App.tsx中

代码语言:javascript
复制
import './utils/Strings'

我检查过的其他一些链接是thesethese,但它们都不起作用。在解决问题时,任何帮助都会受到感谢。

EN

回答 1

Stack Overflow用户

发布于 2022-03-19 22:32:26

我在这方面也挣扎了一段时间.在决定在组件之外的东西上使用i18n.t()之前,我的测试还不错。小丑不喜欢这样

基于这个答案,我让它运行https://github.com/i18next/i18next/issues/1426#issuecomment-828656983

这里有一个对我有用的解决方案:

代码语言:javascript
复制
// setupTests.ts
jest.mock("react-i18next", () => ({
  // this mock makes sure any components using the translate hook can use it without a warning being shown
  useTranslation: () => {
    return {
      t: (str: string) => str,
      i18n: {
        changeLanguage: () => new Promise(() => {}),
      },
    };
  },
  initReactI18next: {
    type: "3rdParty",
    init: jest.fn(),
  },
}));

编辑:没有看到adrai的评论,他首先提到了,但为了清楚起见,我将保留显示所有文件内容的答案

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71532601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档