i18next 缺少密钥但找到了其他密钥

发布于 2025-01-17 12:20:58 字数 1257 浏览 1 评论 0原文

我什么都不懂了。

我使用 i18next 并想要访问我的密钥,但它不起作用。

i18next::translator: missingKey de translation profile profile
i18next::translator: missingKey de translation profile profile
i18next::translator: missingKey de translation profile profile

这是我的 Translation.json

{
  "app_name": "Test (NEW DE)",
  "profile": "ss"
}

这有效:

<Text>{ t('app_name') }</Text>

这不起作用:

<Text>{ t('profile') }</Text>

任何人都可以解释我为什么它不能正常工作吗?

i18下一个

import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';

i18next
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    react: {
      useSuspense: false
    },
    fallbackLng: 'en',
    compatibilityJSON: 'v3',
    lng: 'de',
    debug: process.env.NODE_ENV === 'development',
    backend: {
      loadPath: `http://192.168.0.249:4000/public/locales/{{lng}}/translation.json`,
    },
    interpolation: {
      escapeValue: false,
    },
    keySeparator: '.',
  });

export default i18next;

I dont understand nothing anymore.

I use i18next and want to access to my key but it does not work.

i18next::translator: missingKey de translation profile profile
i18next::translator: missingKey de translation profile profile
i18next::translator: missingKey de translation profile profile

this is my translation.json

{
  "app_name": "Test (NEW DE)",
  "profile": "ss"
}

this works:

<Text>{ t('app_name') }</Text>

this not working:

<Text>{ t('profile') }</Text>

can anyone explain me why its not working correctly ?

i18next

import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';

i18next
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    react: {
      useSuspense: false
    },
    fallbackLng: 'en',
    compatibilityJSON: 'v3',
    lng: 'de',
    debug: process.env.NODE_ENV === 'development',
    backend: {
      loadPath: `http://192.168.0.249:4000/public/locales/{{lng}}/translation.json`,
    },
    interpolation: {
      escapeValue: false,
    },
    keySeparator: '.',
  });

export default i18next;

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

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

发布评论

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

评论(3

热血少△年 2025-01-24 12:20:58

我怀疑,您太早调用 t 函数并且没有等待翻译完全加载。

如果您不使用 Suspense: useSuspense: false 您需要确保检查就绪标志。

https://react.i18next.com/latest/usetranslation-hook#不使用挂起

// additional ready will state if translations are loaded or not
const { t, i18n, ready } = useTranslation('ns1');

https://react.i18next.com/latest/withtranslation-hoc#not-using-suspense

// use tReady prop in MyComponent to check if translations
// are already loaded or not
const ExtendedComponent = withTranslation()(MyComponent);
<ExtendedComponent />

I suspect, you're calling the t function too early and are not waiting for the translations to be fully loaded.

If you're not using suspense: useSuspense: false you need to make sure you check for the ready flag instead.

https://react.i18next.com/latest/usetranslation-hook#not-using-suspense

// additional ready will state if translations are loaded or not
const { t, i18n, ready } = useTranslation('ns1');

https://react.i18next.com/latest/withtranslation-hoc#not-using-suspense

// use tReady prop in MyComponent to check if translations
// are already loaded or not
const ExtendedComponent = withTranslation()(MyComponent);
<ExtendedComponent />
乖乖 2025-01-24 12:20:58

这是因为下划线不能以关键名称为单位。不可能有任何特殊角色。该名称需要是骆驼才能正常工作。

It's because of the underscore cannot be in the key name. There can't be any special characters. The name needs to be camelcase for it to work.

去了角落 2025-01-24 12:20:58
.init({
    fallbackLng: 'fa',
    debug: false,  // add this code 
  })
.init({
    fallbackLng: 'fa',
    debug: false,  // add this code 
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文