Django translation escape% sign

I am trying to translate text containing a percent sign like this:

{% 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" 

That at compilation does not transfer the text to German. I tried doing {% trans "100%% butterfly" %} , but this makes the pages display a "100 %% butterfly" when viewed in German and English. I also tried using blocktrans tags instead to translate text with the same result.

Manually removing the extra% in the .po file along with the line #, fuzzy, python-format , but I wouldn’t do this for every% sign I am trying to translate.

How can I avoid this in my HTML so that Django will stop generating fuzzy translations in the .po file and not get confused thinking that I am trying to format in python?

+6
source share
3 answers

Turns out this is a Django issue with a fix possibly appearing in Django 1.9. One solution:

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

Which essentially covers python formatting, which he thinks you're trying to do when you put the% character in your text. It is verbose and sloppy, but it works.

+1
source

According to this comment in Django Trac, adding a translator comment to deactivate the python format above the line you want to translate may fix / this issue.

If the text to translate is in your Python code, use:

 # Translator: xgettext:no-python-format _('100% butterfly') 

For the trans template tag, you can try:

 {# Translators: xgettext:no-python-format #} {% trans "100% butterfly" %} 

as described in the document .

+3
source

I think the best way is to use the html code: %

This is the most elegant way to work with django translation, for example.

0
source

All Articles