首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取未定义的i18next

获取未定义的i18next
EN

Stack Overflow用户
提问于 2020-06-18 01:27:34
回答 1查看 3K关注 0票数 3

我正在初始化i18next,但遇到错误:

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'use' of undefined

以下是我的代码:

代码语言:javascript
复制
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;

react: 16.13.1

i18next: 19.4.5

EN

回答 1

Stack Overflow用户

发布于 2020-06-18 01:45:37

在挂载应用程序节点的index.js中,您是否也在那里使用import './i18n'?对于我的设置,除了您显示的文件之外,还需要该文件。

代码语言:javascript
复制
// 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文件,它是我自己的配置,用于加载和初始化我的设置

代码语言:javascript
复制
// 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
    }
  });

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

https://stackoverflow.com/questions/62434894

复制
相关文章

相似问题

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