Why there must be a msgid tag in ngettext?
From the first glance, it is not clear why we should use msgid tag for the first argument of ngettext function.
The main reason for this is to be able to use ttag without babel transpile.
Details:
Valid ngettext usage:
import { ngettext, msgid } from 'ttag'
function test(n) {
return ngettext(msgid`${n} time clicked`, `${n} times clicked`, n)
}
For instance, we have a universal(isomorphic) application and our translations must be applied on the server side at a runtime. So, ttag should be able to match an appropriate translation without additional transpilation steps. To find the translation it should get a proper msgid from ngettext's first argument. Let's imagine we are executing the function described above:
test(3);
Inside ngettext function we will receive 3 time clicked
instead of ${n} time clicked
.
The problem is, that expressions in the template have been already evaluated. So there is no opportunity to find out what template we used before evaluation in our source strings. That is the reason, why there must be some kind of a tag that will save msgid before evaluation.
You can find out more about es6 tagged templates - here