Python 如何使用mako模板引擎添加i18n功能?
我正在做一个样本来测试这一点 myprj/setup.pyPython 如何使用mako模板引擎添加i18n功能?,python,internationalization,pyramid,mako,python-babel,Python,Internationalization,Pyramid,Mako,Python Babel,我正在做一个样本来测试这一点 myprj/setup.py requires = [ 'pyramid', 'pyramid_mako == 1.0.2', 'mako', 'Babel', 'lingua', ] setup(name='myprj', ... messages_extractors = { '.': [ ('**.py', 'lingua_python', None ), ('templ
requires = [
'pyramid',
'pyramid_mako == 1.0.2',
'mako',
'Babel',
'lingua',
]
setup(name='myprj',
...
messages_extractors = { '.': [
('**.py', 'lingua_python', None ),
('templates/**.html', 'mako', None ),
('templates/**.mako', 'mako', None ),
('static/**', 'ignore', None ),
]},
)
myprj/setup.cfg
[compile_catalog]
directory = myprj/locale
domain = myprj
statistics = true
[extract_messages]
add_comments = TRANSLATORS:
output_file = myprj/locale/myprj.pot
width = 80
[init_catalog]
domain = myprj
input_file = myprj/locale/myprj.pot
output_dir = myprj/locale
[update_catalog]
domain = myprj
input_file = myprj/locale/myprj.pot
output_dir = myprj/locale
previous = true
myprj/babel.cfg
[python: myprj/**.py]
[mako: myprj/templates/**.html]
input_encoding = utf-8
myprj/myprj/\uuuuu init\uuuuuu.py
def main(...):
...
config.add_translation_dirs('myprj.locale')
myprj/views.py
from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/mytemplate.html')
def my_view(request):
return {'project': 'myprj'}
myprj/myprj/templates/mytemplate.html
<body>
${_('search_documentation')}
</body>
在我将.po文件编译为.mo文件并运行项目之后。它表明
pyramid_mako.MakoRenderingException
MakoRenderingException:
Traceback (most recent call last):
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/pyramid_mako-1.0.2-py2.7.egg/pyramid_mako/__init__.py", line 148, in __call__
result = template.render_unicode(**system)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/template.py", line 454, in render_unicode
as_unicode=True)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 829, in _render
**_kwargs_for_callable(callable_, data))
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 864, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 890, in _exec_template
callable_(context, *args, **kwargs)
File "/Users/username/test/myprj/myprj/templates/mytemplate.html", line 5, in render_body
${_('search_documentation')}
TypeError: 'Undefined' object is not callable
我已经阅读了mako关于巴贝尔的文件:
但我不知道如何使用它。我在.po文件中编写了msgstr
。它没有找到那条路吗
树
pyramid_mako.MakoRenderingException
MakoRenderingException:
Traceback (most recent call last):
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/pyramid_mako-1.0.2-py2.7.egg/pyramid_mako/__init__.py", line 148, in __call__
result = template.render_unicode(**system)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/template.py", line 454, in render_unicode
as_unicode=True)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 829, in _render
**_kwargs_for_callable(callable_, data))
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 864, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/Users/username/.virtualenvs/myprj/lib/python2.7/site-packages/Mako-1.0.3-py2.7.egg/mako/runtime.py", line 890, in _exec_template
callable_(context, *args, **kwargs)
File "/Users/username/test/myprj/myprj/templates/mytemplate.html", line 5, in render_body
${_('search_documentation')}
TypeError: 'Undefined' object is not callable
(myprj)➜ myprj tree
.
...
├── babel.cfg
├── development.ini
├── production.ini
├── myprj
│ ├── __init__.py
│ ├── locale
│ │ ├── en
│ │ │ └── LC_MESSAGES
│ │ │ ├── myprj.mo
│ │ │ └── myprj.po
│ │ ├── myprj.pot
...
│ ├── templates
│ │ └── mytemplate.html
...
├── setup.cfg
└── setup.py