#37486 assigned Bug

makemessages fails to extract context from multiline translation tags

Reported by: Patel Namraa Owned by: Patel Namraa
Component: Internationalization Version: dev
Severity: Normal Keywords:
Cc: Patel Namraa Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
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 (0)

Note: See TracTickets for help on using tickets.
Back to Top