Opened 3 weeks ago

Last modified 4 days ago

#37486 new Bug

makemessages fails to extract context from multiline translation tags

Reported by: Patel Namraa Owned by:
Component: Internationalization Version: dev
Severity: Release blocker Keywords:
Cc: Patel Namraa, Abdulhafiz Davlatov, helmidev03 Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The makemessages command doesn't correctly extract contextual translations from multiline {% translate %} and {% blocktranslate %} template tags.

For example:

{% blocktranslate
    with name=user.name
    context "greeting"
%}
    Hello {{ name }}
{% endblocktranslate %}

The template parser correctly recognizes the message context at runtime. However, the extraction logic in django/utils/translation/template.py doesn't recognize contexts spanning multiple lines because the regular expressions used for parsing translation tags don't match newline characters.

Consequently, makemessages may extract the message without its context, producing an incorrect gettext entry. This is inconsistent with the contextual translation lookup performed at runtime.

There is also a source-location issue. Newlines inside multiline template tags aren't preserved in the output generated by templatize(). As a result, subsequent messages can receive incorrect line numbers in generated .po files.

This can also affect the association of translator comments with extracted messages.

Proposed fix

The regular expressions used for extracting translation contexts should support multiline tags.

Additionally, the original newlines inside template tags should be preserved during templatization so that gettext receives accurate source locations.

Regression tests should cover:

  • Multiline {% translate %} tags with message contexts.
  • Multiline {% blocktranslate %} tags with message contexts.
  • Correct extraction of contextual message IDs.
  • Preservation of source line numbers after multiline tags.

The issue can be reproduced using templatize() directly, and the proposed changes include regression tests covering the affected extraction behavior.

Change History (10)

comment:1 by Sarah Boyce, 2 weeks ago

Severity: Normal → Release blocker
Triage Stage: Unreviewed → Accepted

Replicated thank you! Refs #35899

comment:2 by Abdulhafiz Davlatov, 2 weeks ago

Has patch: set

comment:3 by Abdulhafiz Davlatov, 2 weeks ago

Cc: Abdulhafiz Davlatov added

comment:4 by Abdulhafiz Davlatov, 2 weeks ago

comment:5 by Sarah Boyce, 2 weeks ago

Patch needs improvement: set

comment:6 by Johannes Maron, 2 weeks ago

Has patch: unset
Owner: Patel Namraa removed
Patch needs improvement: unset
Status: assigned → new

comment:7 by helmidev03, 2 weeks ago

Cc: helmidev03 added
Has patch: set

comment:8 by helmidev03, 2 weeks ago

Owner: set to helmidev03
Status: new → assigned

comment:9 by David Smith, 4 days ago

Owner: helmidev03 removed
Status: assigned → new

comment:10 by David Smith, 4 days ago

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top