Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pybabel generates empty pot file with jinja2

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 <[email protected]>\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?

like image 592
Or Duan Avatar asked Apr 21 '15 07:04

Or Duan


4 Answers

I encountered the same problem and in my case the issue was specifying input files, i.e. files where to look for strings marked for translation.

When I run pybabel extract -F babel.cfg -o messages.pot as suggested in various tutorials (for example this one) I got pybabel: error: no input files or directories specified error.

The command that finally worked for me was:

pybabel extract -F babel.cfg -o messages.pot --input-dirs=.
like image 71
Sergii Gryshkevych Avatar answered Sep 28 '22 07:09

Sergii Gryshkevych


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.

like image 28
Or Duan Avatar answered Sep 28 '22 06:09

Or Duan


For others who may not be using the Assets Extension as referred in the accepted answer, the problem could lie with the babel.cfg file itself.

For me the problem was with my babel.cfg file which I had taken from this tutorial.

Later when I switched to using the babel.cfg file right from the question here, things worked out nicely for me.

Just for record, here is the babel.cfg file which worked for me.

[python: **.py]
[jinja2: **.html]
encoding = utf-8
extensions=jinja2.ext.autoescape,jinja2.ext.with_
like image 29
bhaskarc Avatar answered Sep 28 '22 06:09

bhaskarc


I encountered a similar problem:

input:

pybabel extract -F babel.cfg -k _l -o messages.pot

output:

Usage: pybabel extract [options] <input-paths>

pybabel: error: no input files or directories specified

My solution is just to add two spaces and a period after input command.

pybabel extract -F babel.cfg -k _l -o messages.pot .

Hope this helps.

Update:

My babel.cfg file is:

[python: **.py] [jinja2: **.html] extensions=jinja2.ext.autoescape,jinja2.ext.with_

like image 38
kheng oon Low Avatar answered Sep 28 '22 07:09

kheng oon Low