简体   繁体   English

使用react-i18next useTranslationHooks时出现“ i18next backendConnector:加载命名空间失败”

[英]“i18next backendConnector: loading namespace failed” when using react-i18next useTranslationHooks

I'm trying to implement react-i18next on my create-react-app application with useTranslation() hooks. 我正在尝试使用useTranslation()钩子在我的create-react-app应用程序上实现react-i18next。

However, in the console, the debug mode shows 但是,在控制台中,调试模式显示为

i18next::backendConnector: loading namespace billingAddress for language en failed failed parsing /locales/en/billingAddress.json to json i18next :: backendConnector:为语言en加载名称空间billingAddress失败,无法将/locales/en/billingAddress.json解析为json

and

i18next::translator: missingKey en billingAddress Form.email Customer Email (Default) i18next :: translator:missingKey和billingAddress Form.email客户电子邮件(默认)

i18next.ts i18next.ts

import {initReactI18next} from "react-i18next";
import i18next from "i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-xhr-backend';
import XHR from "i18next-xhr-backend";

i18next
// .use(Backend)
    .use(LanguageDetector)
    .use(XHR)
    .use(initReactI18next) // passes i18n down to react-i18next
    .init({
        lng: "en",
        fallbackLng: {
            'en-US':['en'],
            'zh-CN':['cn'],
            default:['en']
        },
        debug: true,
        ns: ['billingAddress'],
        defaultNS: 'billingAddress',
        // load: "currentOnly",
        interpolation: {
            escapeValue: false // react already safes from xss
        },
        keySeparator: false,
        // backend: {
        //     loadPath: "/locales/{{lng}}/{{ns}}.json",
        //     crossDomain: true
        // },
        react: {
            wait: true,
        }
    });

export default i18next;

files 档案

├── src
│   ├── i18next.ts
│   ├── locales
│   │   ├── cn
│   │   │   └── billingAddress.ts
│   │   └── en
│   │       └── billingAddress.ts
│   ├── index.tsx

version

"react": "^16.8.4", “ react”:“ ^ 16.8.4”,

"i18next": "^15.0.7", “ i18next”:“ ^ 15.0.7”,

my translation file 我的翻译文件

export default{
  "Form": {
    "emailLabel": "customer Email",
    "emailPlaceholder": "email@emial.com",
  }
}

my billingAddress file 我的billingAddress文件

  const [t, i18n] = useTranslation();

    const changeLanguage = (language: string) => (e: React.MouseEvent) => {
        i18n.changeLanguage(language)
    };

const someValues: billingAddress = {
emailLabel: t("Form.emailLabel","Customer Email")
}

return (
<div>
 <button onClick={changeLanguage("en")}>English</button>
<OtherComponent value={someValues} />
</div>
)

I'm quite confuse how to load namespace now... any ideas? 我很困惑现在如何加载名称空间...有什么想法吗?

Thanks in advance! 提前致谢!

You might just follow the sample here: https://github.com/i18next/react-i18next/tree/master/example/react/public/locales 您可能只在这里关注示例: https : //github.com/i18next/react-i18next/tree/master/example/react/public/locales

Using xhr backend you need to provide valid JSON files as source - that's all. 使用xhr后端,您需要提供有效的JSON文件作为源-仅此而已。

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

相关问题 i18next backendConnector:加载 fr 语言的命名空间翻译失败 - i18next backendConnector: loading namespace translations for language fr failed 每当我更改命名空间时反应 i18next::backendConnector 问题 - React i18next::backendConnector issue whenever I change the namespace 使用“ react-i18next”和“ react-router-dom”时出现“ i18next:missingKey” - “i18next: missingKey” whilst using 'react-i18next' and 'react-router-dom' react-i18next:: 你需要使用 initReactI18next 传入一个 i18next 实例 - react-i18next:: You will need to pass in an i18next instance by using initReactI18next 反应&i18next,为什么我需要react-i18next - react & i18next, why do i need react-i18next 如何在带有 MDXJS 的 MDX/Markdown 文件中使用 i18next/react-i18next? - How to use i18next / react-i18next inside MDX / Markdown files with MDXJS? react-i18next:: 您需要使用 Jest 单元测试中引发的 initReactI18next 警告传入 i18next 实例 - react-i18next:: You will need to pass in an i18next instance by using initReactI18next warning thrown in Jest unit test 在反应 i18next 中将参数传递到命名空间 - Pass a parameter into namespace in react i18next react-i18next 和 typescript 4.1 - react-i18next and typescript 4.1 react-i18next:全局组件 - react-i18next: Global Components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM