Babel doesn't recognize jinja2 extraction method for language support

谁说胖子不能爱 提交于 2019-12-05 07:45:12

I saw that your question was still unanswered. Your problem looks similar to what I got after reinstalling my development environment:

$ pybabel extract -F babel.cfg -o messages.pot .
extracting messages from admin.py
:
extracting messages from templates/404.html (extensions="jinja2.ext.autoescape,jinja2.ext.with_")
Traceback (most recent call last):
  File "/usr/local/bin/pybabel", line 9, in <module>
    load_entry_point('Babel==0.9.6', 'console_scripts', 'pybabel')()
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 1107, in main
    return CommandLineInterface().run(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 651, in run
    return getattr(self, cmdname)(args[1:])
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 912, in extract
    for filename, lineno, message, comments in extracted:
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 171, in extract_from_dir
    strip_comment_tags):
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 201, in extract_from_file
    strip_comment_tags))
  File "/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 270, in extract
    raise ValueError('Unknown extraction method %r' % method)
ValueError: Unknown extraction method 'jinja2'

It turned out that I had forgotten to install jinja2. Since the server environment had it installed I didn't notice it first. After installing jinja2 with:

$ sudo pip install jinja2

the extraction would complete:

$ pybabel extract -F babel.cfg -o messages.pot .
extracting messages from admin.py
:
extracting messages from test/item_tests.py
writing PO template file to messages.pot

Babel relies on the jinja2 entry point being set up in the egg info.

To workaround that, change this in you cfg file:

# Extraction from Jinja2 template files  
[jinja2: **.html]

to that:

# Extraction from Jinja2 template files  
[jinja2.ext:babel_extract[i18n]: **.html]

I had same problem. I solved it by upgrading setuptools from 20.7.0 to more fresh version:

sudo pip install --upgrade setuptools
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!