8

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Or Duan
  • 13,142
  • 6
  • 60
  • 65

4 Answers4

12

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=.
Sergii Gryshkevych
  • 4,029
  • 1
  • 25
  • 42
7

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.

Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • 9
    Setting `silent=False` in your `babel.cfg` will help pinpoint the problem: thing is, this should really be the default :( Thanks for pointing out the webassets Jinja2 extension! – Manuel Oct 30 '15 at 23:01
  • 1
    Phew, this was an obscure bug -- thank you for posting your fix, worked perfectly. – rlafuente Sep 06 '17 at 17:23
4

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_
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
3

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_