简体   繁体   English

React i18next 缺少 React 应用程序的关键问题

[英]React i18next missing key problem with React app

I'm trying to implement a multi language site on my website but all I'm getting is that I'm missing a key and I really don't know why.我正在尝试在我的网站上实现一个多语言网站,但我得到的只是我丢失了一个密钥,我真的不知道为什么。

Here is my i18n config file这是我的 i18n 配置文件

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    lng: 'en',
    resources: {
      en: {
        translation: { key: 'value' },
      },
      es: {
        translation: { key: 'value' },
      },
    },
    fallbackLng: 'en',
    debug: true,
    ns: ['translations'],
    defaultNS: 'translations',
    keySeparator: false,
    interpolation: {
      escapeValue: false,
      formatSeparator: ',',
    },
    react: {
      wait: true,
      useSuspense: false,
    },
  });

export default i18n;

and my index.jsx looks like this我的 index.jsx 看起来像这样

import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import { I18nextProvider } from 'react-i18next';
import Home from './pages/Home';
import Gallery from './pages/Gallery';
import NotFound from './pages/404';
import UnderConstruction from './pages/UnderConstruction';
import i18n from './i18n';

ReactDOM.render(
  <React.StrictMode>
    <I18nextProvider i18n={i18n}>
      <Suspense fallback={null}>
        <BrowserRouter>
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/gallery" element={<Gallery />} />
            <Route path="/about-us" element={<UnderConstruction />} />
            <Route path="*" element={<NotFound />} />
          </Routes>
        </BrowserRouter>
      </Suspense>
    </I18nextProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);

And I'm trying to use the translation like this我正在尝试使用这样的翻译

import '../assets/scss/header.scss';
import { useEffect, useState } from 'react';
import { useTranslation, withTranslation } from 'react-i18next';
import { ReactComponent as MenuIcon } from '../assets/images/menu.svg';
import Logo from '../assets/images/dmeh-logo.jpg';
import LangSelector from './LangSelector';

function Navbar({ t }) {
  const [click, setClick] = useState(false);
  const handleClick = () => setClick(!click);
  // const { t } = useTranslation();
  // console.log(t);
  const [show, setShow] = useState(false);
  const controlNavbar = () => {
    if (window.scrollY <= 100) {
      setShow(false);
    } else {
      setShow(true);
    }
  };

  useEffect(() => {
    window.addEventListener('scroll', controlNavbar);
    return () => {
      window.removeEventListener('scroll', controlNavbar);
    };
  }, []);

  return (
    <nav className={`navbar ${show && 'nav__shrink'}`}>
      <div className="logo-nav">
        <div className="logo-container">
          <div className="logo-navbar">
            <a href="/">
              <img src={Logo} alt="" />
            </a>
          </div>
        </div>
      </div>

      <div className={click ? 'menu active' : 'menu'}>
        <LangSelector click={click} />
        <div className="categories-options">
          <ul className={click ? 'nav-options active' : 'nav-options'}>
            <li className="nav-option">
              {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
              <a href="/#services">
                {' '}
                {t('key')}
                {' '}
              </a>
            </li>
          </ul>
        </div>
      </div>
      <div className="mobile-menu">
        <button type="button" onClick={handleClick}>
          <MenuIcon className="flag-icon" />
        </button>
      </div>
    </nav>
  );
}

export default withTranslation()(Navbar);

I've tried without the withTranslation and still nothing.我试过没有withTranslation ,但仍然没有。

i18next::translator: missingKey en translations key key

this is one of the messages I'm getting and the other is这是我收到的消息之一,另一个是

It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.

but this last one is just a warning from the console但这最后一个只是来自控制台的警告

thank you in advance.先感谢您。

Set the correct namespace:设置正确的命名空间:

ns: ['translation'],
defaultNS: 'translation',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM