React-I18Next显示了关键名称而不是它的值

发布于 2025-02-04 01:11:57 字数 1450 浏览 2 评论 0原文

我正在尝试为我的React-Creat-App项目使用不同的语言,并且面临着这个问题,其中关键名称在DOM中显示而不是其值。

index.js看起来像这样:

import React from "react";
import ReactDOM from "react-dom";
import "./index.scss";
import App from "./components/App/App";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import HttpApi from "i18next-http-backend";

i18n
  .use(initReactI18next)
  .use(LanguageDetector)
  .use(HttpApi)
  .init({
    debug: true,
    fallbackLng: "ar",
    detection: {
      order: ["htmlTag", "cookie"],
      caches: ["cookie"],
    },
    backend: {
      loadPath: "../public/locales/{{lng}}/translation.json",
    },
    react: { useSuspense: false },
  });

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

locales/ar/translation.json看起来像这样:

{
    "meet_team": "تعرفوا على فريقنا"
}

locales/tr/translation.json看起来像这样:

{
    "meet_team": "ekipimizle tanışın"
}

我想翻译的组件看起来像这样:

import { useTranslation } from 'react-i18next';

const OurTeam = () => {
  const { t } = useTranslation();
  return <h1 className="foo">{t("meet_team")}</h1>;
};

页面上显示的内容是:

meet_team

我该如何解决此问题?

I'm trying to use different languages for my react-creat-app project and I'm facing this problem where the key name is shown in DOM instead of its value.

index.js looks like this :

import React from "react";
import ReactDOM from "react-dom";
import "./index.scss";
import App from "./components/App/App";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import HttpApi from "i18next-http-backend";

i18n
  .use(initReactI18next)
  .use(LanguageDetector)
  .use(HttpApi)
  .init({
    debug: true,
    fallbackLng: "ar",
    detection: {
      order: ["htmlTag", "cookie"],
      caches: ["cookie"],
    },
    backend: {
      loadPath: "../public/locales/{{lng}}/translation.json",
    },
    react: { useSuspense: false },
  });

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

locales/ar/translation.json looks like this :

{
    "meet_team": "تعرفوا على فريقنا"
}

locales/tr/translation.json looks like this :

{
    "meet_team": "ekipimizle tanışın"
}

The component I want to translate looks like this :

import { useTranslation } from 'react-i18next';

const OurTeam = () => {
  const { t } = useTranslation();
  return <h1 className="foo">{t("meet_team")}</h1>;
};

what is displayed on the page is this :

meet_team

How can I resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2025-02-11 01:11:57

问题是在index.js

 loadPath: "../public/locales/{{lng}}/translation.json",

将其更改为:

loadPath: "/locales/{{lng}}/translation.json", 

已解决问题

The problem was at index.js :

 loadPath: "../public/locales/{{lng}}/translation.json",

changing it to :

loadPath: "/locales/{{lng}}/translation.json", 

has fixed the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文