Handling percent-sign (%) in Django blocktrans tags
Asked Answered
P

4

6

I'm currently localizing my Django app. All other translations work fine except percent-sign inside blocktrans tags.

In my template I have {% blocktrans %}Original % blocktrans{endblocktrans %}.

django-admin makemessages produces this in django.po:

#: templates/index.html:78
#, python-format
msgid "Original %% blocktrans"
msgstr ""

I update that to msgstr "Translated %% blocktrans", run django-admin compilemessages, restart dev server and refresh the page, but I still see Original % blocktrans in the output. Other translations are shown properly.

For reference, {% trans "Original % trans" %} also works ok. After makemessages and translation I have:

#: templates/index.html:72
msgid "Original % trans"
msgstr "Translated % trans"

This works as expected - translated version is shown.

I must use blocktrans because I also need to embed variables to the strings. I'm using Django 1.2.5.

How can I make blocktrans work with percent-signs?

Pelvis answered 27/3, 2011 at 8:59 Comment(0)
P
3

Couldn't find a real solution to the problem, so I used a workaround: create a constant PERCENT_SIGN = u'%' and use that as {{ PERCENT_SIGN }} inside blocktrans-blocks.

Pelvis answered 13/4, 2011 at 8:16 Comment(5)
What about using html entity % ?Skyros
That would probably work, but I think it would be even more confusing to the translator than {{ PERCENT_SIGN }}, though shorter. Also, I'm using this for plain text email templates, so the entity is a no-go.Pelvis
Right.. Well, the best solution would be to fix blocktrans in Django trunk :)Skyros
Or just like that: {% blocktrans with percent="%" %} This is 100{{ percent }} working! {% endblocktrans %}Tremolite
Facing that issue, I decided to use the HTML entity % which is less confusing than %Mehalek
I
3

Check out this ticket - it's not a solution, but it sheds light on what's going on

Isolationist answered 27/3, 2011 at 10:9 Comment(0)
P
3

Couldn't find a real solution to the problem, so I used a workaround: create a constant PERCENT_SIGN = u'%' and use that as {{ PERCENT_SIGN }} inside blocktrans-blocks.

Pelvis answered 13/4, 2011 at 8:16 Comment(5)
What about using html entity % ?Skyros
That would probably work, but I think it would be even more confusing to the translator than {{ PERCENT_SIGN }}, though shorter. Also, I'm using this for plain text email templates, so the entity is a no-go.Pelvis
Right.. Well, the best solution would be to fix blocktrans in Django trunk :)Skyros
Or just like that: {% blocktrans with percent="%" %} This is 100{{ percent }} working! {% endblocktrans %}Tremolite
Facing that issue, I decided to use the HTML entity % which is less confusing than %Mehalek
D
2

Another ticket has been opened for this particular issue, with a patch that fixes it. Hopefully it will be fixed for Django 1.4.

https://code.djangoproject.com/ticket/16721

Dendro answered 29/8, 2011 at 19:2 Comment(0)
C
0

In 2023 the doc mention this problem. It looks like you must have %% also in the translated phrase. Hence # xgettext:no-python-format is a bit useless, since the translators must be aware of that anyway. Using %% everywhere works.

Coppersmith answered 11/2 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.