2

I am using Django and cannot get the correct plural form in my template using French, whatever text I use in my blocktrans tag. For example:

{% blocktrans count counter=0 %}
    foo
{% plural %}
    bars
{% endblocktrans %}

Gives me bars instead of foo. Indeed, 0 is singular in French.

I checked the language code with the following in my template:

{% get_current_language as LANGUAGE_CODE %}
{{ LANGUAGE_CODE }}

It gives me fr.

The following is in the header of my django.pofile for French translation:

"Plural-Forms: nplurals=2; plural=(n > 1);\n"

Do you have any idea guys?

Q Caron
  • 952
  • 13
  • 26

1 Answers1

3

Okay I finally found what is going on here. Because of the way I typed the blocktrans, here is what I got in the django.po file generated by the python manage.py makemessages -l fr command:

msgid ""
"\n"
"                                foo\n"
"                            "
msgid_plural ""
"\n"
"                                bars\n"
"                            "
msgstr[0] ""
msgstr[1] ""

And I put something like that as msgstr[0] and msgstr[1]:

msgstr[0] ""
"\n"
"                                foo\n"
"                            "
msgstr[1] ""
"\n"
"                                bars\n"
"                            "

So I updated the template from:

{% blocktrans count counter=0 %}
    foo
{% plural %}
    bars
{% endblocktrans %}

To:

{% blocktrans count counter=0 %}foo{% plural %}bars{% endblocktrans %}

With the following translation:

msgid "foo"
msgid_plural "bars"
msgstr[0] "foo"
msgstr[1] "bars"

It looks like if there is something wrong (the translation strings is missing or empty) in your django.po then it results in a bad 0 interpretation in template.

I'll leave it there... Hope this helps!

Q Caron
  • 952
  • 13
  • 26
  • 2
    I also found that if you do not translate your blocktrans and test in FR (or other languages), 0 (zero) display the plural. Once translated, everything went fine :-) – anou Oct 20 '16 at 20:43