【问题标题】:How to use react-i18-next on .json or .js file?如何在 .json 或 .js 文件上使用 react-i18-next?
【发布时间】:2021-10-06 15:19:42
【问题描述】:

我知道如何在react组件中使用i18-next,但现在我想做的是我想从我的js文件或json文件中导入一些数据,只是为了代码分离,但我不能使用在这些文件中使用翻译挂钩,那我该怎么办?这就是我想要实现的目标。

数据.js


export const slideInfo = [
    {
        id:1,
        img:"https://i.imgur.com/g2weSNQ.png",
        title:t{"title"} //how to use something like const { t } = useTranslation() at here
    },
    {
        id:2,
        img:"https://i.imgur.com/g2weSNQ.png",
        title:""
    },
    {
        id:3,
        img:"https://i.imgur.com/g2weSNQ.png",
        title:""
    },
]

或者json文件中的同样的东西,我不介意数据存储在哪里,只是想将我的代码分开以便于阅读。

【问题讨论】:

    标签: javascript reactjs i18next


    【解决方案1】:

    一个可能的解决方案是:

    export const getSlideInfo = (t) => [
      {
        id: 1,
        img: 'https://i.imgur.com/g2weSNQ.png',
        title: t('title'),
      },
      ...
    ];
    
    const ReactComponent = () => {
      const { t } = useTranslation();
      const slideInfo = getSlideInfo(t);
    };
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 2020-07-18
      • 2021-02-04
      • 2023-03-08
      • 2020-08-27
      • 2022-07-21
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多