Flask jinja filter `assets` is incompatible with the `pybabel` command and fails to extract translations
See original GitHub issueThere’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 called index.html. I extract with the command
pybabel extract -F babel.cfg --charset=utf=8 -o messages.pot --no-wrap index.html
The file bable.cfg
[jinja2: **.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
My env:
(venv) heartsucker@pythagoras:~/Downloads/test$ python --version
Python 3.4.2
(venv) heartsucker@pythagoras:~/Downloads/test$ pip freeze
Babel==2.3.4
click==6.6
Flask==0.11.1
Flask-Babel==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
pytz==2016.6.1
Werkzeug==0.11.10
(venv) heartsucker@pythagoras:~/Downloads/test$ uname -a
Linux pythagoras 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2+deb8u3 (2016-07-02) x86_64 GNU/Linux
(venv) heartsucker@pythagoras:~/Downloads/test$
What works
If index.html is this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{{ ASSET_URL }}" />
</head>
<body>
{{ gettext('Test') }}
</body>
</html>
The messages.pot is this:
# Translations template for PROJECT.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-08-25 21:57+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf=8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"
#: index.html:7
msgid "Test"
msgstr ""
The one string called Test is correctly extracted.
What doesn’t work
If index.html is this:
<!DOCTYPE html>
<html>
<head>
{% assets filters="cssmin", output="gen/source.css", "css/normalize.css" %}
<link rel="stylesheet" href="{{ ASSET_URL }}" />
{% endassets %}
</head>
<body>
{{ gettext('Test') }}
</body>
</html>
The messages.pot is this:
# Translations template for PROJECT.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-08-25 21:58+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf=8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"
The one string called Test is not extracted.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Pybabel generates empty pot file with jinja2 - Stack Overflow
I encountered the same problem and in my case the issue was specifying input files, i.e. files where to look for strings marked...
Read more >Babel Documentation - Read the Docs
As gettext provides a solid and well supported foundation for translating application messages, Babel does not reinvent the wheel, but rather ...
Read more >Flask-Babel 1.0.0 documentation - GitHub Pages
A semi-colon ( ; ) separated string of absolute and relative (to the app root) paths to translation folders. Defaults to translations ....
Read more >invenio - PyPI
Allows using variables from application config for building asset bundles. legacy. Improves exception handling of integrity errors raised by MySQLdb library.
Read more >gettext — Multilingual internationalization services — Python ...
The interface described below allows you to write your module and application messages in one natural language, and provide a catalog of translated...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Anyone using the
compresstag from Jina Assets Compressor (https://github.com/jaysonsantos/jinja-assets-compressor) can useOtherwise it fails with:
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
silentbehavior by default, so you don’t get sensible error messages about these cases. (You can addsilent=falseto thejinja2section 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:
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