使用 i18n 返回 JSON 翻译内容数组
Returning a JSON array of translated content using i18n
上下文: 我正在使用 ReactJS 的 react-i18next
模块制作一个多语言页面。
问题: 我无法访问排列的 JSON 内容。
每种语言的翻译内容都存储在单独的 JSON 文件中,非数组内容工作正常并且可以正确显示和翻译,但是,我不能'我的 React 组件似乎没有使用数组内容,更不用说通过 console.log()
.
访问其内容了
下面是我的 translationEN.json
文件的示例:
{
"name": "Test",
"occupations":["occupation1",
"Part-time occupation2",
"Language enthusiast"]
}
我可以使用 i18n.t("name")
来引用非数组 name
。
但是,尝试使用 i18n.t("occupations")
访问我的数组 occupations
结果如下 console.log:
key 'occupations (en)' returned an object instead of string.
使用 JSON.stringify()
不能解决问题,i18next's documentation
上建议的 console.log(i18n.t("occupations"), { returnObjects: true })
也不能
提前致谢!
问题已解决。
可以这样访问数组值:i18n.t("occupations.0")
表示 occupation 1
,i18n.t("occupations.1")
表示 Part-time occupation 2
,i18n.t("occupations.2")
表示 Language enthusiast
。
我只需要把它循环出来让它看起来更干净。
上下文: 我正在使用 ReactJS 的 react-i18next
模块制作一个多语言页面。
问题: 我无法访问排列的 JSON 内容。
每种语言的翻译内容都存储在单独的 JSON 文件中,非数组内容工作正常并且可以正确显示和翻译,但是,我不能'我的 React 组件似乎没有使用数组内容,更不用说通过 console.log()
.
下面是我的 translationEN.json
文件的示例:
{
"name": "Test",
"occupations":["occupation1",
"Part-time occupation2",
"Language enthusiast"]
}
我可以使用 i18n.t("name")
来引用非数组 name
。
但是,尝试使用 i18n.t("occupations")
访问我的数组 occupations
结果如下 console.log:
key 'occupations (en)' returned an object instead of string.
使用 JSON.stringify()
不能解决问题,i18next's documentation
console.log(i18n.t("occupations"), { returnObjects: true })
也不能
提前致谢!
问题已解决。
可以这样访问数组值:i18n.t("occupations.0")
表示 occupation 1
,i18n.t("occupations.1")
表示 Part-time occupation 2
,i18n.t("occupations.2")
表示 Language enthusiast
。
我只需要把它循环出来让它看起来更干净。