Closed josevalim closed 9 years ago
Ok, I think I got it. I'll start working on this :)
Hey there @josevalim, quick question (I'm posting here so that others can benefit from the answer as well :smiley:).
I implemented lgettext
with support for interpolation and I'm now implementing lngettext
; should we still provide an argument n
to lngettext
which is the number of elements or should we extract that from the bindings passed to lngettext
, standardizing on a name like %{count}
?
Example:
msgid "One error"
msgid_plural "%{count} errors"
msgstr[0] "Un errore"
msgstr[1] "%{count} errori"
With the example above, should we call lngettext(_, _, "One error", "%{count} errors", 4, %{})
(no bindings and count
as the standard for the number of elements), lngettext(_, _, "One error", "%{count}", count: 4)
or the most explicit lngettext(_, _, "One error", "%{count} errors", 4, count: 4)
?
I would go with lngettext(_, _, "One error", "%{count} errors", 4, %{})
which will automatically set count: 4
.
Although gettext does not support interpolation, we could support it out of the box. The reasoning is that interpolation is common and supporting it out of the box is going to allow us to do some optimizations similar to the ones Chris did in linguist.
The idea is that
lgettext
andlngettext
will receive an extra argument with interpolation: a map or a list (where a list is internally converted to a map). A naïve way of supporting interpolation is this:However, it would be better if we extracted all occurrence of "%{count}" or "%{whatever}" from strings during compilation time. In other words, we could compile to something like:
Where
missing_interpolation_key
will look at the interpolation map, the required keys, and return a string saying: "missing interpolation keys: foo, bar, something".Note though we still need to have the runtime behaviour, because the default messages need to be translated at runtime, as we never compile them. So the fallback clause still is:
Thoughts?