babel icon indicating copy to clipboard operation
babel copied to clipboard

JInja extraction fails silently without error if there's an unrecognized do statement with {a:b} inside

Open johnzhou721 opened this issue 6 months ago • 3 comments

See the following MWE.

[templates/test.html]

{% set a = "foo" %}
{% set b = "bar" %}
{% set d = [1, 2, 3] %}
{% set mydict = {} %}

{% for c in d %}
  {% do mydict.update({a: b}) %}
{% endfor %}

{{ _("Hello!") }}

Deleting the entirely nonsense block before _("Hello") makes it able to extract Hello, but in its current state, no messages are extracted.

[babel.cfg]

[jinja2: templates/**.html]
encoding=utf-8

Anyways: Babel 2.17.0 is used.

johnzhou721 avatar Jun 29 '25 14:06 johnzhou721

I think the actual issue is that you're using a {% do %} statement, which is implemented as a Jinja extension.

No extensions are loaded by default unless you tell the Jinja extractor to; as documented in the Jinja docs, you would need

[jinja2: templates/**.html]
encoding = utf-8
extensions = jinja2.ext.do

to have the extension (and thus the do statement, and thus no parsing error from Jinja) loaded for extraction.

akx avatar Jul 08 '25 10:07 akx

But I think we at least an error message, not just stay silent about it.

johnzhou721 avatar Jul 08 '25 13:07 johnzhou721

Something like "unknown statement do"

johnzhou721 avatar Jul 08 '25 13:07 johnzhou721