tayden tayden - 2 months ago 15x
Python Question

Django translation escape % sign

I'm trying to translate some text that contains a percent sign like so:

{% trans "100% butterfly" %}

When I run the makemessages command, I get the following output in my german .po file:

#: .\appName\templates\appName\butterflies.html:54
#, fuzzy, python-format
#| msgid ""
#| "100% butterfly"
msgid ""
"100%% butterfly"
msgstr ""
"100% shmetterling"

Which when compiled, fails to translate the text to German. I've tried doing
{% trans "100%% butterfly" %}
, but this causes the pages to display "100%% butterfly" when viewed in both german and english. I've also tried using blocktrans tags instead to translate the text, with the same result.

Manually erasing the extra % in the .po file, along with the
#, fuzzy, python-format
line works, but I'd rather not have to do this for every % sign I'm trying to translate.

How do I escape this in my HTML so that Django stops generating a fuzzy translation in the .po file and doesn't get confused thinking I'm trying to do some python formatting?


Turns out this is a Django problem with a fix possibly coming in Django 1.9. One workaround is to do:

{% blocktrans with percent="%" %}100{{percent}} butterfly{% endblocktrans %}

Which essentially embraces the python formatting it thinks you're trying to do when you put a % symbol in your trans text. It's verbose and sucks, but it works.