【问题标题】:Getting i18next undefined获取 i18next 未定义
【发布时间】:2020-10-07 14:52:47
【问题描述】:

我正在初始化 i18next 但出现错误:

Uncaught TypeError: Cannot read property 'use' of undefined

以下是我的代码:

import i18n from 'i18next';// Getting eslint error Module imports itself.
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

const fallbackLng = ['en'];
const availableLanguages = ['en', 'ar', 'fr'];

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng,

    detection: {
      checkWhitelist: true,
    },

    debug: false,

    whitelist: availableLanguages,

    interpolation: {
      escapeValue: false,
    },
  });

export default i18n;

反应:16.13.1

i18next:19.4.5

【问题讨论】:

  • react 的版本是多少?
  • 我已编辑问题请检查
  • 你确定你是通过 npm 或者 yarn 添加了这些包吗?我安装了以下“i18next”:“^17.3.1”、“i18next-browser-languagedetector”:“^3.1.1”、“i18next-xhr-backend”:“^3.2.2”、
  • 是使用纱线安装

标签: javascript reactjs i18next react-i18next


【解决方案1】:

在安装 App 节点的 index.js 中,您是否也有import './i18n' 在那里?对于我的设置,除了您显示的文件之外,这也是必需的。

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {unregister} from './serviceWorker.js'
import './i18n';

ReactDOM.render(<App />, document.getElementById('root'));
unregister()

我有一个 i18n.js 文件,这是我自己的配置,用于使用我的设置进行加载和初始化

// i18n.js
// https://codesandbox.io/s/react-i18next-basic-example-with-usetranslation-hook-l05ml

import i18n from "i18next";
import Backend from "i18next-xhr-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
import moment from 'moment/moment'
import 'moment/min/locales';

i18n
  // load translation using xhr -> see /public/locales
  // learn more: https://github.com/i18next/i18next-xhr-backend
  .use(Backend)
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    fallbackLng: "en-GB",
    detection: {
      // https://github.com/i18next/i18next-browser-languageDetector
      order: ['navigator'],
    },
    debug: false,
    interpolation: {      
       escapeValue: false, // not needed for react as it escapes by default
       format: function (value, format, lng) {
        if (format === 'uppercase') return value.toUpperCase();
        if (value instanceof Date) { 
          var result =  moment(value).locale(lng).format(format);
          return result;
        }
        if (format === 'intlDate') {
          return new Intl.DateTimeFormat(lng).format(value);
        }
        return value;
      },
      defaultVariables : {
        product: "Word Pigeon"
      }
    },
    // keySeparator: '_',
    react: {
      wait: true,
      escapeValue: false // not needed for react as it escapes by default
    }
  });

【讨论】:

  • 是的,我以相同的方式导入,但它不起作用。我已经从另一个项目中复制了代码,它在那里工作,但不是在这里
  • 尝试删除 node_modules 并再次安装 yarn(对不起,我知道这是显而易见的尝试)
  • 您是否将语言环境添加到您的公用文件夹app-root\public\locales\en-GB(将 en-GB 替换为您的默认语言环境),并使用带有翻译的 translation.json 文件
猜你喜欢
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多