I'm using Flask, Flask-Babel and Jinja2. I'm trying to generate the .pot file. That what I did so far.
My babel.cfg looks like this:
[python: **.py]
[jinja2: **.html]
encoding = utf-8
extensions=jinja2.ext.autoescape,jinja2.ext.with_
I'm initialize Flask-Babel with my app like this:
# Babel init
babel = Babel(app)
app.config['BABEL_DEFAULT_LOCALE'] = 'en'
In my templates directory I do have homepage.html:
...
<div class="news-wrapper">
<p class="quote">{{ gettext('Hey there') }}</p>
<p class="quote">{% trans %}Trying 42{% endtrans %}</p>
<p class="quote">{{ _('Maybe like this?') }}</p>
</div>
...
Then running this command(while I'm in my virtualenv):
pybabel extract -F babel.cfg -o messages.pot .
One of the outputs lines is:
extracting messages from templates/homepage.html (extensions="jinja2.ext.autoescape,jinja2.ext.with_", encoding="utf-8")
And it is generates a messages.pot file with this content:
# Translations template for PROJECT.
# Copyright (C) 2015 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2015.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2015-04-21 10:06+0300\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 1.3\n"
The file does not have my translations.
I get no errors or warnings, pybabel just can't find the gettext
variations in jinja2 files. BUT when I use the gettext
in a .py file it is working just fine.
Do I miss something?
After spending hours to figure it out I found the solution, posting for future help if someone will need it.
I had Flask-Assets installed in my templates, apparently if you have it, you need to add in you babel.cfg
:
extensions=jinja2.ext.autoescape,jinja2.ext.with_,webassets.ext.jinja2.AssetsExtension
From the Flask-Assets docs:
Otherwise, babel will not extract strings from any templates that include an assets tag.
I'm going to do a pull request that checks if you have Flask-Assets and Flask-Babel installed and you didn't added the right extension, It will give you warning - I think the developer should get some error/warning.