[Django] #33440: Cannot escape % character when using gettext

140 views
Skip to first unread message

Django

unread,
Jan 13, 2022, 10:43:29 AM1/13/22
to django-...@googlegroups.com
#33440: Cannot escape % character when using gettext
-----------------------------------------+------------------------
Reporter: Stian Jensen | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Reproduce:
Write something like `gettext('This item is 50% off!')`

run ./manage.py makemessages -a

find the string in the .po file and translate it like this:

#: templates/test.html:2
#, python-format
msgid "This item is 50% off!"
msgstr "Denne har 50% rabatt!"

run ./manage.py compilemessages

get this error message:

Execution of msgfmt failed: /Users/stiaje/Projects/django-
backend/app/hyre/locale/nb/LC_MESSAGES/django.po:601: format
specifications in 'msgid' and 'msgstr' for argument 1 are not the same

It works if the letter after the percentage is o in all languages, but
fails otherwise.

I tried escaping the percentage symbol as %%, but that resulted in %%
being outputted on runtime.

I tried using \u00e0 instead, but that gave:

Execution of msgfmt failed: /Users/stiaje/Projects/django-
backend/app/hyre/locale/nb/LC_MESSAGES/django.po:980:36: invalid control
sequence

I see that a very similar issue has been reported before, but it is
closed, with a comment saying to open a new issue for followups:
https://code.djangoproject.com/ticket/11240

I can make compilemessages work by manually removing python-format from
the strings with %, but makemessages will just add that back the next time
I run it.

--
Ticket URL: <https://code.djangoproject.com/ticket/33440>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 14, 2022, 12:35:59 AM1/14/22
to django-...@googlegroups.com
#33440: Cannot escape % character when using gettext().
-------------------------------------+-------------------------------------

Reporter: Stian Jensen | Owner: nobody
Type: Uncategorized | Status: closed
Component: | Version: 3.2
Internationalization |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Claude Paroz, Ramiro Morales (added)
* resolution: => invalid
* status: new => closed
* component: Uncategorized => Internationalization


Comment:

#11240 is about a similar issue but with `{% translate %}` template tag.
The main difference is that by design we don't support %-formatting in `{%
translate %}` (see [https://code.djangoproject.com/ticket/11240#comment:16
comment]), which is not the case in `gettext()`. `gettext()` supports
`%-formatting` and `%o` is a valid conversion type (signed octal value) so
there is not much we can do. I'd recommend to remove `50%` from translated
string (it's not something that will change in different languages), e.g.
{{{
gettext('This item is %(discount)s off!') % {'discount': '50%'}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33440#comment:1>

Reply all
Reply to author
Forward
0 new messages