i18next-parser icon indicating copy to clipboard operation
i18next-parser copied to clipboard

Is there ability to generate i18next keys inside map function

Open olehkhalin opened this issue 4 years ago • 1 comments

I have a map that take titles from an array of objects

Content

export const StatsData: StatsDataType[] = [
  {
    id: 0,
    title: 'TEST 0',
    value: 20000000000,
    change: 32,
  },
  {
    id: 1,
    title: 'TEST 1',
    value: 20000000000,
    change: 32,
  },
  {
    id: 2,
    title: 'TEST 2',
    value: 20000000000,
    change: 32,
  },
  {
    id: 3,
    title: 'TEST 3',
    value: 20000000000,
    change: 32,
  },
  {
    id: 4,
    title: 'TEST 4',
    value: 20000000000,
    change: 32,
  },
];

Component

<div className={s.cards}>
  {
    StatsData.map(({
      id, title, value, change,
    }) => (
      <StatsCard
        key={id}
        title={t(`home:${title}`)}
        value={value}
        change={change}
      />
    ))
  }
</div>

Expected behavior

Expecting get generated keys inside locales folder

Your Environment

  • runtime version: node v16
  • next version: 11.0.1
  • i18next-parser version: 4.2.0
  • os: Mac

olehkhalin avatar Jul 08 '21 11:07 olehkhalin

This is a code parser, not a code interpreter. There is no way we can do what you want as we never run the code and writing a tool that does what you want would be a tremendous amount of work (I don't know of any that does).

That being said, some lexers might support parsing comments. So you can do something like this:

var variableKey = "my key";
t(variableKey); // will not be parsed since it is not a string litteral
// t("my key"); this might be parsed 

I'm saying "might" because we change the code parser at some point and I can't remember if this still works.

karellm avatar Jul 09 '21 11:07 karellm