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 , 2 weeks ago
| Severity: | Normal → Release blocker |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 2 weeks ago
| Has patch: | set |
|---|
comment:3 by , 2 weeks ago
| Cc: | added |
|---|
comment:5 by , 2 weeks ago
| Patch needs improvement: | set |
|---|
comment:6 by , 2 weeks ago
| Has patch: | unset |
|---|---|
| Owner: | removed |
| Patch needs improvement: | unset |
| Status: | assigned → new |
comment:7 by , 2 weeks ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:8 by , 2 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:9 by , 4 days ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:10 by , 4 days ago
| Patch needs improvement: | set |
|---|
Replicated thank you! Refs #35899