Opened 13 minutes ago
#37564 new Bug
`_("...")` template literals containing `%` render a doubled `%%` and don't match the msgid that `makemessages` extracts
| Reported by: | Thibaut Decombe | Owned by: | |
|---|---|---|---|
| Component: | Uncategorized | Version: | 5.2 |
| Severity: | Low | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
A _("...") string containing % is breaking translation (but it works for {% translate %}):
{% load i18n %} {% translate "100%" %} {# 100% #} {{ _("100%") }} {# 100%% #}
It also can't be translated: makemessages extracts msgid "100%", but at render time Django looks up "100%%":
>>> from django.utils.translation.template import templatize >>> templatize('{% load i18n %}{% translate "100%" %}{{ _("100%") }}') "... gettext(u'100%%') _(\"100%\")"
Seems to be due to #24257 fix (b7508896): it doubled % in Variable.resolve(), which _() also goes through, but undid it only in {% translate %}, and doubled % at extraction only for {% translate %}.
Suggested fix: treat _() like {% translate %}: undo the %% after gettext in Variable.resolve(), and double % in _() literals in templatize().
Found while porting the template engine in django-rusty-templates (discussion).