6

我正在为我的项目添加语言翻译支持。代码在 Python 上,在 html 文件中有 jinja2 和 Javascript。

我正在尝试使用 Babel 进行翻译,但它不识别 jinja2 的提取方法。也许我使用了不正确的名称。

这是我的ini文件:

# Extraction from Python source files
[python: **.py]
# Extraction from Jinja2 template files
[jinja2: **.html]
# Extraction from JavaScript files
[javascript: **.js]
extract_messages = $._, jQuery._

这是我收到的错误;

C:\>python Babel-0.9.6/babel/messages/frontend.py extract --project=GV --version=1 --no-location -o locale\messages.pot -F babel.ini frontend te
mplates
extracting messages from frontend\__init__.py
INFO:babel:extracting messages from frontend\__init__.py
...
Traceback (most recent call last):
  File "Babel-0.9.6/babel/messages/frontend.py", line 1208, in <module>
    main()
  File "Babel-0.9.6/babel/messages/frontend.py", line 1107, in main
    return CommandLineInterface().run(sys.argv)
  File "Babel-0.9.6/babel/messages/frontend.py", line 651, in run
    return getattr(self, cmdname)(args[1:])
  File "Babel-0.9.6/babel/messages/frontend.py", line 912, in extract
    for filename, lineno, message, comments in extracted:
  File "c:\Python27\lib\site-packages\babel-0.9.6-py2.7.egg\babel\messages\extract.py", line 172, in extract_from_dir
    strip_comment_tags):
  File "c:\Python27\lib\site-packages\babel-0.9.6-py2.7.egg\babel\messages\extract.py", line 202, in extract_from_file
    strip_comment_tags))
  File "c:\Python27\lib\site-packages\babel-0.9.6-py2.7.egg\babel\messages\extract.py", line 271, in extract
    raise ValueError('Unknown extraction method %r' % method)
ValueError: Unknown extraction method 'jinja2'
Press any key to continue . . .

有任何想法吗?谢谢,加迪

4

3 回答 3

14

我看到你的问题仍然没有答案。您的问题与我重新安装开发环境后遇到的问题相似:

$ 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'

原来我忘记安装jinja2了。由于服务器环境已经安装了它,我没有首先注意到它。安装jinja2 后:

$ sudo pip install jinja2

提取将完成:

$ 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
于 2012-11-20T10:50:58.800 回答
2

Babel 依赖于在 egg info 中设置的 jinja2 入口点。

要解决此问题,请在您的 cfg 文件中进行更改:

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

对此:

# Extraction from Jinja2 template files  
[jinja2.ext:babel_extract[i18n]: **.html]
于 2013-12-07T14:43:42.347 回答
1

我有同样的问题。我通过将 setuptools 从 20.7.0 升级到更新鲜的版本来解决它:

sudo pip install --upgrade setuptools
于 2018-01-13T15:15:32.123 回答