【发布时间】:2022-03-29 03:20:35
【问题描述】:
我已尝试在我的 react 应用程序中实现 react-i18next,但它并没有给出我希望它工作的感觉
控制台没有显示任何错误。它只是不翻译。
谁有类似的问题,可以帮我弄清楚为什么它不能翻译?
我的i18n.js文件内容:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import translationPL from './translations/pl/common.json';
const resources = {
pl: {
translation: translationPL
}
};
i18n
.use(initReactI18next)
.init({
resources: resources,
lng: "pl",
defaultNS: 'common',
keySeparator: false,
interpolation: {
escapeValue: false
}
});
export default i18n;
然后是我的App.js 文件
import React from 'react';
import './App.css';
import './i18n';
import { withTranslation } from 'react-i18next';
class App extends React.Component {
.
.
.
render() {
const { t } = this.props;
return (
button className="btn btn-primary" onClick={() => this.endMeeting()}><span className="code-tag">{ t('End meeting') }</span></button>
);
}
}
export default withTranslation('common')(App);
最后是./translations/pl/common.json
{
"End meeting": "Zakończ posiedzenie"
}
更新
启用调试选项后,它会给我这个输出:
i18next: languageChanged pl
i18next: initialized {debug: true, initImmediate: true, ns: Array(1), defaultNS: "common", fallbackLng: Array(1), …}
i18next::translator: missingKey pl common key_name key_name
i18next::translator: missingKey pl common key_name key_name
【问题讨论】:
-
尝试将 i18next 调试选项设置为 true
-
@adrai 调试:languageChanged -> 初始化 -> missingKey
-
把 withTranslation('common') 改成 withTranslation('translation') 那就是你的 ns 名字
标签: reactjs i18next react-i18next