11

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?

tayden
  • 570
  • 7
  • 20
  • 3
    Looks like doing `{% trans "100% butterfly" %}` does the trick, but might be confusing for the translators. – tayden Oct 08 '15 at 18:23
  • 1
    Did you try using `"100%% shmetterling"` in the .po file for the msgstr? – Joey Wilhelm Oct 08 '15 at 18:25
  • Just now, yes. I get "100%% shmetterling" as the rendered html output – tayden Oct 08 '15 at 18:27
  • 1
    There's a related ticket [#24257](https://code.djangoproject.com/ticket/24257), which is fixed in the upcoming Django 1.9. The creator of that ticket said they used blocktrans as a work around, but I see you already tried that. – Alasdair Oct 08 '15 at 18:29

3 Answers3

8

According to this comment in Django's Trac, adding a translator comment to deactivate python format above the string that you want to translate can fix / workaround this issue.

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

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

For trans template tag, you can try:

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

as explained in the doc.

zzheng
  • 898
  • 11
  • 17
2

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

It is the most elegant way for dealing with django translation for example.

Guillaume Cisco
  • 2,859
  • 24
  • 25
  • That's true for developers, but again, is confusing for translators who don't understand the html code – tayden Jan 28 '16 at 15:26
  • Yes, but sadly, you cannot do it with django javascript translation. ```%``` and warning your translators is the best way today. – Guillaume Cisco Feb 01 '16 at 11:15
1

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.

tayden
  • 570
  • 7
  • 20