Question

In our code, we have the word "Photo" marked for translation in singular. At a different position in the code, we have this word in pluralized translation "Photo" / "Photos", liek so:

1 {% trans 'Photo' %}
2 {% trans 'Photos' %}

and

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

Possibly, we must use our counter variable x inside the translation strings. However, I couldn't find anything about such requirement in the docs. Anyway, with our code, all we get in our PO files is:

msgid "Photos"
msgstr ""

msgid "Photo"
msgid_plural "Photos"
msgstr[0] ""
msgstr[1] ""

There is no msgid for "Photo", resulting in "Photo" not being translated at all, since the actual translation string does not exist - except when used in *n*gettext, but not in {% trans 'Photo' %}.

Am I doing something wrong here? Is it a Django bug?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top