Kai021195
Kai021195

Reputation: 833

How to use react-i18-next on .json or .js file?

I know how to use i18-next in react components, but right now what I want to do is that I want to import some data from my js file or json file, just for code separation, but I can't use useTranslation hook in those files, then what should I do ? here's what I want to achieve.

data.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:""
    },
]

Or the same thing in json file , I don't mind where the data is stored , just want to separate my code for readability.

Upvotes: 0

Views: 305

Answers (1)

tcconstantin
tcconstantin

Reputation: 389

A possible solution would be:

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);
};

Upvotes: 1

Related Questions