如何使用mako模板引擎添加i18n功能?

时间:2016-01-21 08:56:23

标签: internationalization pyramid template-engine mako babel

我正在做一个样本来测试它。

myprj / setup.py

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/__init__.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 /模板/ mytemplate.html

<body>
    ${_('search_documentation')}
</body>

myprj / myprj /区域/ EN / LC_MESSAGES / myprj.po

msgid "search_documentation"
msgstr "Search Documentation"

将.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关于babel的文件:

  

http://docs.makotemplates.org/en/latest/usage.html#babel

但我不知道如何使用它。我在.po文件中写了msgstr。它没找到那条路吗?

(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

0 个答案:

没有答案