[Django]-Weird behavior of Django translation of one word in plural and singular form

8đź‘Ť

âś…

It’s not a bug in Django — you are trying to translate the term “Photo” two different ways, in two different places. Once as a plain (number-agnostic) term, and once as a number-aware term. There is no way to represent that in a PO file, which can only have one entry with msgid “Photo”.

(Note that “Photos” is handled differently in your case, since as far as gettext is concerned, it is only translated once, as a plain term.)

Plural forms can be very different across languages, and you shouldn’t just be trying to localize the single noun “Photo” in this case. Instead, you should be localizing the term “x Photo(s)” for the singular and plural case in each language.

(In English, that would be “1 Photo” and “{x} Photos”, but in other languages, you could have more or less than two, and the number itself may not even go before the word for “photos”, which is why you have to localize the whole term)

In your template, then, you should have:

{% blocktrans count counter=x %}{{ count }} Photo{% plural %}{{ count }} Photos{% endblocktrans %}

Then your PO file should contain lines like this:

msgid "%(count)s Photo"
msgid_plural "%(count)s Photos"
msgstr[0] ""
msgstr[1] ""

And you can localize msgstr[0] (singular case) and msgstr[1] (plural case) for each language. Some languages will require more than just [0] and [1], but gettext should take care of that for you when it generates the PO file for that language.

👤Ian Clelland

Leave a comment