首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react i18Next useTranslationHooks时,"i18next backendConnector:装入命名空间失败“

使用react i18Next useTranslationHooks时,"i18next backendConnector:装入命名空间失败“
EN

Stack Overflow用户
提问于 2019-03-26 05:01:36
回答 1查看 10.2K关注 0票数 8

我正在尝试用useTranslation()钩子在我的创建-反应-应用程序应用程序上实现Reacti-i18Next。

但是,在控制台中,调试模式显示

i18next::backendConnector:为语言en加载名称空间billingAddress失败解析/locales/en/billingAddress.json到json

翻译: missingKey en billingAddress Form.email客户电子邮件(默认)

i18next.ts

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

文件

代码语言:javascript
复制
├── src
│   ├── i18next.ts
│   ├── locales
│   │   ├── cn
│   │   │   └── billingAddress.ts
│   │   └── en
│   │       └── billingAddress.ts
│   ├── index.tsx

版本

“反应”:"^16.8.4", "i18next":"^15.0.7“

我的翻译档案

代码语言:javascript
复制
export default{
  "Form": {
    "emailLabel": "customer Email",
    "emailPlaceholder": "email@emial.com",
  }
}

我的billingAddress文件

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

我现在很困惑如何加载命名空间..。有什么想法吗?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2019-03-26 15:35:11

您可能只需遵循下面的示例:https://github.com/i18next/react-i18next/tree/master/example/react/public/locales

使用xhr后端,您需要提供有效的JSON文件作为源代码--仅此而已。

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

https://stackoverflow.com/questions/55350136

复制
相关文章

相似问题

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