简体   繁体   English

使用 i18n 返回翻译内容的 JSON 数组

[英]Returning a JSON array of translated content using i18n

Context: I am making a multi-language page using the react-i18next module of ReactJS.上下文:我正在使用 ReactJS 的react-i18next模块制作多语言页面。

Issue: I cannot access the arrayed JSON content.问题:我无法访问排列的 JSON 内容。

The translated content being stored in separate JSON files for each language, non-array content works fine and gets correctly displayed and translated , however, I can't seem to use the arrayed content on my React components, let alone access its content through console.log() .翻译后的内容存储在每种语言的单独 JSON 文件中,非数组内容工作正常并且可以正确显示和翻译,但是,我似乎无法在我的 React 组件上使用数组内容,更不用说通过console.log()访问它的内容了console.log()

Below is an example of my translationEN.json file:以下是我的translationEN.json文件的示例:

{
  "name": "Test",
  "occupations":["occupation1",
  "Part-time occupation2",
  "Language enthusiast"]
}

I am being able to refer to the non-array name using i18n.t("name") .我能够使用i18n.t("name")来引用非数组name However, attempting to access my arrayed occupations using i18n.t("occupations") results in the following console.log:但是,尝试使用i18n.t("occupations")访问我排列的occupations会导致以下 console.log:

key 'occupations (en)' returned an object instead of string.

Using JSON.stringify() doesn't solve the issue, neither does console.log(i18n.t("occupations"), { returnObjects: true }) as suggested oni18next's documentation使用JSON.stringify()并不能解决问题, console.log(i18n.t("occupations"), { returnObjects: true })也没有按照i18next 的文档中的建议

Thanks in advance!提前致谢!

Problem solved.问题解决了。 The array values can be accessed as such: i18n.t("occupations.0") for occupation 1 , i18n.t("occupations.1") for Part-time occupation 2 and i18n.t("occupations.2") for Language enthusiast .数组值可以这样访问: i18n.t("occupations.0")用于occupation 1i18n.t("occupations.1")用于Part-time occupation 2i18n.t("occupations.2") Language enthusiast I just need to loop it out to make it look cleaner.我只需要把它循环出来让它看起来更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM