ttag-org / ttag

:orange_book: simple approach for javascript localization
https://ttag.js.org/
MIT License
337 stars 40 forks source link

ngettext with a regular string #281

Open uladzimirdev opened 4 months ago

uladzimirdev commented 4 months ago

Hi, I couldn't find the answer from the code, so maybe you can help me to understand how to handle such simple case

ngettext(msgid`record`, "records", n);

babel plugin doesn't like such strings and raises an error

at template2Msgid (/node_modules/babel-plugin-ttag/dist/utils.js:116:32)
at /node_modules/babel-plugin-ttag/dist/extractors/ngettext.js:55:38

is it a bug or just unexpected usage? is there any workaround?

AlexMost commented 4 months ago

Hi, seems like a bug. As a temporary workaround you can try to use

ngettext(msgid`record`, `records`, n);

The most frequent case for ngettext is when n is present in the translated string like

ngettext(msgid`${ n } record`, `${ n } records`, n);

But I agree that your example should also work fine. Will take a look at this case ASAP.

uladzimirdev commented 4 months ago

fair enough, I had this problem only because of eslint quotes rule, which replaced backticks with double quotes as there wasn't any parameter inside.

Thanks for confirming