reactjs - 在 React 中设置 i18next 我做错了什么?收到“i18next::translator: missingKey”错误
问题描述
在我src
的文件夹中,我创建了一个名为 的文件夹i18n
,其中包含这三个文件
en.json
es.json
pl.json
这就是他们的样子:
{
"selectAction": "Select Action",
"workflow": "Workflow",
"details": "Details"
}
在src
文件夹中,我还添加了这个名为i18n.js
import i18n from 'i18next'
import LanguageDetector from "i18next-browser-languagedetector"
import { initReactI18next } from 'react-i18next'
// import Backend from "i18next-xhr-backend";
// import XHR from 'i18next-xhr-backend'
import languageEn from './i18n/en.json'
import languageEs from './i18n/es.json'
import languagePl from './i18n/pl.json'
i18n
// .use(Backend)
// .use(XHR)
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
en: languageEn,
es: languageEs,
pl: languagePl
},
lng: "es",
fallbackLng: "en",
debug: true,
keySeparator: ".",
interpolation: {
escapeValue: false,
}
});
console.log(i18n.languages)
export default i18n;
我在 src 根目录中的 index.js 文件如下所示:
// import statements are omitted, but I am importing I18NextProvider and i18n
ReactDOM.render(
<I18nextProvider i18n={i18n}>
<App />
</I18nextProvider>
document.getElementById('root')
);
最后,在我的应用程序的某个地方,我有这个:
// Other imports omitted
import { useTranslation } from 'react-i18next';
const DetailsPlaceholder = () => {
const { t } = useTranslation();
return (
<h4 className="zero-state-section-text">{t('selectAction')}</h4>
);
};
export default DetailsPlaceholder;
当我尝试加载页面时,我看到键 'selectAction' 作为文本(而不是真实文本),并且记录了这个错误:
i18next::translator: missingKey es translation selectAction selectAction
解决方案
translation
所有资源文件都应在key下存储字符串(如快速入门中的react-i18next
):
{
"translation": {
"selectAction": "Select Action",
"workflow": "Workflow",
"details": "Details"
}
}
这是一个带有重现错误和固定配置的存储库:
https ://github.com/terales/reproduce-react-i18next-missingkey-error
推荐阅读
- python - PySpark:难以使用 mapreduce 函数实现 KMeans
- python - 仅当窗口有超过 x 个可用数据时才使用滚动函数查找平均值
- c - C / 从文本文件中获取行字符串并将它们存储在数组/指针中
- python - 抓取被阻塞,得到缓冲区读取器的响应
- lua - Lua:当我尝试在 Windows 中运行脚本时出现错误
- ios - 使用 Apple 登录未在签名和可比性菜单中显示
- node.js - 如何从猫鼬返回的对象中删除属性
- python - Keras 损失函数值错误:ValueError: An operation has `None` for gradient。在 LSTM 网络上
- themes - 无法启动 Visual Studio 扩展的调试器
- c# - 如何将大量几何图形(形成一个字符串的线条)水平排列以从线条中制作正确的字符串?