Closed heartsucker closed 7 years ago
Hey,
Sorry it's taken us a while to get back to you.
What's happening here is that Jinja fails to parse the template with the {% assets %}
tag (not a filter; a custom filter wouldn't cause problems), since the extension that provides it is not loaded.
Unfortunately the Jinja extractor has silent
behavior by default, so you don't get sensible error messages about these cases. (You can add silent=false
to the jinja2
section in your babel.cfg to have it fail loudly.)
The easiest fix is to augment your babel.cfg to add the assets extension -- I'm not sure how you configure it in your Flask app, so I can't directly help further:
[jinja2: **.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_,whatever.import.path.there.is.to.your.assets.extension
Another, slightly more involved but also more DRY and future-proof, way to fix this would be to use a extractor that uses the environment you already set up for the Flask app for Jinja files; this is what my django-i18nkit project does for compatibility with Django-Jinja: https://github.com/akx/django-i18nkit/blob/master/i18nkit/jinja2_extract.py
@heartsucker Did you get things to work with the above instructions? :)
Closing due to inactivity.
For Pelican users who ran into this, the assets path should be
extensions=webassets.ext.jinja2.AssetsExtension
Thanks to Or Duan at Stack Overflow for this answer
@rlafuente I use Pelican and try to set my custom jinja extension in babel.cfg in this way:
[jinja2: theme/templates/index.html]
extensions=jinja2.ext.i18n, custom_jinja_extension.IncludeMarkdownExtension
silent = false
But it doesn't work for me. Can you help me with the right settings?
P.S. This is a path to my custom extension /project_folder/custom_jinja_extension.py
It's been more than a year and I really don't remember much about this subject. Maybe the space between the extension names is causing an issue? Otherwise it might be a path problem (it doesn't find your extension because it isn't looking in that dir)? Anyway, that's all I can help with -- good luck!
@rlafuente Do you know how to specify the path rightin babel.cfg?
Anyone using the compress
tag from Jina Assets Compressor (https://github.com/jaysonsantos/jinja-assets-compressor) can use
extensions=jinja2.ext.autoescape,jinja2.ext.with_,jac.extension.CompressorExtension
Otherwise it fails with:
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'compress'.
There's a chance I'm using this wrong, but I'm fairly certain this is a bug. If there is a better way to do this, it is certainly non-intuitive. (originally posted to https://github.com/python-babel/flask-babel/issues/108, but this is probably the right place).
My setup
One
html
file calledindex.html
. I extract with the commandThe file
bable.cfg
My env:
What works
If
index.html
is this:The
messages.pot
is this:The one string called
Test
is correctly extracted.What doesn't work
If
index.html
is this:The
messages.pot
is this:The one string called
Test
is not extracted.