ttag-org / ttag

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

nggettext and msgid returning ${ undefined } on plural #208

Open mmso opened 3 years ago

mmso commented 3 years ago

A string such as

  const result = c('Title').ngettext(msgid`Delete ${name}`, `Delete ${count} contacts`, count);

when translated will result in

${undefined} Kontakte löschen

It seems that variables aren't replaced in the plural version

I've created a reproducible example here: https://github.com/mmso/ttag-bug-example/commit/5a550ddf1aceeb44f4c39c42879e152146145622

AlexMost commented 3 years ago

Thanks for the report! will try to fix that ASAP

zzorba commented 3 years ago

I ran into something that I think might be a similar issue, so just detailing what I found in hopes that it is helpful.

In my codebase, I had two strings with the same key -- one had plural forms and the other did not.

e.g.

ngettext(msgid`${experience} XP`, `${experience} XP`, experience)

vs

t`${experience} XP`

The singular form existed first, and was all that was included in the extracted PO files (even after the plural was added). Even when the singular form was removed, the po files were not updated. Using said translation seemed to cause a runtime error with ngettext + r.replace when it was used.

By deleting the singular form from all the translated versions and regenerating, I was able to get past this.

Not sure if that's helpful, but the singular/plural split seems like it might be related here.

andrey-skl commented 2 months ago

We can see that the issue is replaceVariables takes variables only from first string (that is wrapped with msgid):

2024-05-16 17 14 39