我正在尝试使用命令行工具使用Babel翻译jinja2 / coffin模板。问题是babel_extract函数卡在棺材标签上,如{%url%}或{%csrf_token%}。
首先,为了检测问题,我修改了jinja.ext.babel_extract,并在渲染时在异常时添加了“print e”。
try:
node = environment.parse(source)
tokens = list(environment.lex(environment.preprocess(source)))
except TemplateSyntaxError, e:
# skip templates with syntax errors
print e
return
当我使用Babel运行提取方法时,它会打印出这个错误:
Encountered unknown tag 'url'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'. extracting messages from templates/contact.html (encoding="utf-8")
然后我编辑了我的babel配置以添加棺材库。我还设置了我的环境变量,将我的django项目添加到我的python路径,并将django设置模块设置为我的settings.py。
[python: src/**.py]
[jinja2: templates/**.html]
encoding = utf-8
extensions = coffin.template.Library
[extractors]
jinja2 = jinja2.ext:babel_extract
现在,当我运行extract方法时,会发生以下错误:
extracting messages from templates/brands.html (extensions="coffin.template.Library", encoding="utf-8")
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 "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 1107, in main
return CommandLineInterface().run(sys.argv)
File "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 651, in run
return getattr(self, cmdname)(args[1:])
File "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/frontend.py", line 912, in extract
for filename, lineno, message, comments in extracted:
File "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 171, in extract_from_dir
strip_comment_tags):
File "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 201, in extract_from_file
strip_comment_tags))
File "/Library/Python/2.7/site-packages/Babel-0.9.6-py2.7.egg/babel/messages/extract.py", line 275, in extract
for lineno, funcname, messages, comments in results:
File "/Library/Python/2.7/site-packages/jinja2/ext.py", line 588, in babel_extract
auto_reload=False
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 279, in __init__
self.extensions = load_extensions(self, extensions)
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 76, in load_extensions
result[extension.identifier] = extension(environment)
TypeError: __init__() takes exactly 1 argument (2 given)