gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
159 stars 37 forks source link

Can't get an Array of strings to translate #233

Closed iDVB closed 2 years ago

iDVB commented 2 years ago

Describe the bug

const myArr = ['thing 1', 'thing 2', 'thing 3']
const translatedArr = myArr.map(t)

How to reproduce Trying to get an array in code to translate and extract.

Expected behavior

I would expect that in the translation json file it would have

{
  "thing 1": "thing 1",
  "thing 2": "thing 2",
  "thing 3": "thing 3"
}

What actually happens

There is nothing extracted.

gilbsgilbs commented 2 years ago

The myArr.map(t) happens at runtime. It'd be very cumbersome to parse such complex case using a static analyzer. In your example, you'd need to resolve the myArr reference which'd be even harder than, say, ["thing 1", "thing 2"].map(t). Maybe there should be an entry in the FAQ like "Why doesn't it extract keys in my very-specific use-case?" to explain the whys in details.

The recommended way is : const translatedArr = [t('thing 1'), t('thing 2'), t('thing 3')] or if you need to keep the keys in an array, the plugin should be smart enough to parse this:

const K_THING_1 = 'thing 1';
const K_THING_2 = 'thing 2';
const K_THING_3 = 'thing 3';
const myArr = [K_THING_1, K_THING_2, K_THING_3];
const translatedArr = [t(K_THING_1), t(K_THING_2), t(K_THING_3)];

I don't think it will do more and I don't think it should do more, therefore I'll close this issue as wontfix.

I have a long-term project of adding a plugin API that would allow one to create custom extractors for such specific scenarios. But I'm not sure if and when I'll have the incentives to actually implement that API.