尝试i18next反应我可以加载json文件,但翻译功能返回未更改的字符串

时间:2020-03-13 09:53:15

标签: reactjs i18next react-i18next

这是我的配置文件。控制台日志未显示任何问题。至少与i18next无关。

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

import detector from 'i18next-browser-languagedetector';

const xhrBackendOptions = {
    loadPath: '/locales/{{lng}}/{{ns}}.json',
};
let XHR =  require('i18next-xhr-backend').default;

let backend =  XHR;
let options = {
    fallbackLng: "en",
    whitelist: ["en", "de", "es"],
    ns:["user, claim", "translation"],
    backend:xhrBackendOptions,
    interpolation: {
        escapeValue: false, // not needed for react as it escapes by default
    }
};
console.info("Translation Activated");

i18n
    .use(backend)
    .use(detector)
    .use(initReactI18next)
    .init( options
    );

export default i18n;

这是.jsx文件的一个。语言已正确检测,并且没有任何失败,但函数会返回未更改的字符串

import React from 'react'
import {useSelector} from "react-redux";

import Auth from "ducks/auth";
import SparePart from "ducks/SparePart";

import ViewPort from "components/common/ViewPort";
import ListView from "components/crud/ListView";


import {useHistory} from "react-router";
import {useTranslation} from "react-i18next";

export default function (props) {
    const { t,i18n  } = useTranslation();
    const history = useHistory();
    debugger;
    const isAdmin = useSelector(Auth.selectors.isAdmin);
    const user = useSelector(Auth.selectors.user);
    const elements = useSelector(SparePart.selectors.all);


    return (
        <ViewPort>
            <ListView  key="listView" title={t("spare parts")} model={SparePart} columns={columns} actions={actions}
                       navigable={!isAdmin} editable={isAdmin} />
        </ViewPort>
    );
}

1 个答案:

答案 0 :(得分:0)

似乎translation.json文件有多余的逗号