Search code examples
pythondjangointernationalizationdjango-i18n

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?


Solution

  • 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.