Opened 3 years ago

Closed 3 years ago

#33440 closed Uncategorized (invalid)

Cannot escape % character when using gettext().

Reported by: Stian Jensen Owned by: nobody
Component: Internationalization Version: 3.2
Severity: Normal Keywords:
Cc: Claude Paroz, Ramiro Morales Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (1)

comment:1 by Mariusz Felisiak, 3 years ago

Cc: Claude Paroz Ramiro Morales added
Component: UncategorizedInternationalization
Resolution: invalid
Status: newclosed
Summary: Cannot escape % character when using gettextCannot escape % character when using gettext().

#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 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%'}
Note: See TracTickets for help on using tickets.
Back to Top