1

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

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/templates/mytemplate.html

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

myprj/myprj/locale/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

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

(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
4

0 回答 0