Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep whitespace between {% blocktrans %} and {% plural %} tags without causing msgfmt errors?

I'm rendering some pluralization using the blocktrans tag; here's the relevant snippet in the template file:

{% blocktrans count choice_count=choice_count %}
  You have {{ choice_count }} choice:
{% plural %}
  You have {{ choice_count }} choices:
{% endblocktrans %}

After running python manage.py makemessages --all, this is the relevant snippet in my, e.g. django.po file for en:

msgid ""                                                                        
"\n"                                                                            
"  You have %(choice_count)s choice:\n"                                         
msgid_plural ""                                                                 
"\n"                                                                            
"  You have %(choice_count)s choices:\n"                                        
msgstr[0] "You have one choices:"                                               
msgstr[1] "You have %(choice_count)s choice(s):"   

But when I run python manage.py compilemessages, this is the error message I get:

$ ./manage.py compilemessages 
processing file django.po in /home/yiqing/repos/training/site/training/locale/en/LC_MESSAGES
/home/yiqing/repos/training/site/training/locale/en/LC_MESSAGES/django.po:60: `msgid' and `msgstr[0]' entries do not both begin with '\n'
msgfmt: found 4 fatal errors

I know that it's because of the newlines/spaces in the template file, and I know how to get "around" it -- when I change the the template snippet to, e.g., this:

{% blocktrans count choice_count=choice_count %}You have {{ choice_count }} choice:{% plural %}You have {{ choice_count }} choices:{% endblocktrans %}

And rerun makemessages, remove the fuzzy marker from the messages and then rerun compilemessages, it compiles just fine.

However, my question is how to keep the first template syntax and still be able to compile the messages, because it drastically improves the readability of the code in the template files.

like image 294
3cheesewheel Avatar asked Sep 25 '13 19:09

3cheesewheel


2 Answers

The documentation mentions the "trimmed" keyword for blocktrans, quoting:

For instance, the following {% blocktrans %} tag:

{% blocktrans trimmed %}
  First sentence.
  Second paragraph.
{% endblocktrans %}

will result in the entry "First sentence. Second paragraph." in the PO file, compared to "\n First sentence.\n Second sentence.\n", if the trimmed option had not been specified.

like image 73
Florian Avatar answered Nov 12 '22 23:11

Florian


The simplest thing you can do is match the format of your input strings. In your example, the .po file would look like this:

msgid ""
"\n"
"  You have %(choice_count)s choice:\n"
msgid_plural ""
"\n"
"  You have %(choice_count)s choices:\n"
msgstr[0] "\nYou have one choices:\n"
msgstr[1] "\nYou have %(choice_count)s choice(s):\n"

This file compiles without errors, but, as you see, it is tedious.

As far as I know, there is currently no other workaround for the issue. It appears that django-rosetta does have a patch to handle this exact thing (see https://github.com/mbi/django-rosetta/pull/34).

like image 25
shezi Avatar answered Nov 13 '22 00:11

shezi