在金字塔中使用babel和lingua

时间:2015-05-12 13:11:55

标签: localization internationalization pyramid babel

我有babel和lingua的问题。我希望babel / lingua扫描我的源文件夹以获取特定字符串以创建pot-catalog。

这是我的setup.py

...

requires = [
    ...
    'Babel',
    'lingua',
    ]

extractors = { 'dbas': [
    ('**.py', 'python', None ),
    ('**.pt', 'chameleon', None ),
    ('static/**', 'ignore', None),
    ]}

setup(name='DBAS',
    ...
    message_extractors=extractors,
    ...
    )

我的setup.cfg

[compile_catalog]
directory = dbas/locale
domain = mydbas
statistics = true

[extract_messages]
copyright_holder = Acme Inc.
output_file = dbas/locale/mydbas.pot
charset = UTF-8

[init_catalog]
domain = mydbas
input_file = dbas/locale/mydbas.pot
output_dir = dbas/locale

[update_catalog]
domain = mydbas
input_file = dbas/locale/mydbas.pot
output_dir = dbas/locale
previous = true

在我的 init .py中,我有类似的内容:

config.add_translation_dirs('dbas:locale')

例如我的404模板就是这个:

<!DOCTYPE html>
<html lang="${request.locale_name}"
            metal:use-macro="load: basetemplate.pt"
            xmlns="http://www.w3.org/1999/xhtml"
            xml:lang="en"
            xmlns:i18n="http://xml.zope.org/namespaces/i18n"
            i18n:domain="dbas">

<head>
    <link type="text/css" href="${request.static_url('dbas:static/css/theme_center.css')}" rel="stylesheet">
</head>

<body>

    <div class="center">
        <div class="error">
            <h1><span class="font-semi-bold" i18n:translate="404">404 Error</span></h1>
            <p class="lead font-normal">The page &quot;<span class="font-semi-bold">${page_notfound_viewname}</span>&quot; for could not be found.</p>
            <br>
            <input class="button button-block btn-lg btn btn-primary" type="submit" onClick="self.location.href='/'" value="Let's go home!" />
        </div>
    </div>

</body>
<html>

现在我可以跑了:

python3 setup.py develop
setup.py extract_messages

我收到了:

running extract_messages
extracting messages from dbas/__init__.py
extracting messages from dbas/helper.py
extracting messages from dbas/security.py
extracting messages from dbas/tests.py
extracting messages from dbas/views.py
extracting messages from dbas/database/__init__.py
extracting messages from dbas/database/initializedb.py
extracting messages from dbas/database/model.py
extracting messages from dbas/templates/404.pt
Traceback (most recent call last):
  File "setup.py", line 60, in <module>
    """,
  File "/usr/lib/python3.4/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.4/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.4/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.4/dist-packages/babel/messages/frontend.py", line 305, in run
    for filename, lineno, message, comments, context in extracted:
  File "/usr/local/lib/python3.4/dist-packages/babel/messages/extract.py", line 163, in extract_from_dir
    strip_comment_tags):
  File "/usr/local/lib/python3.4/dist-packages/babel/messages/extract.py", line 190, in extract_from_file
    strip_comment_tags))
  File "/usr/local/lib/python3.4/dist-packages/babel/messages/extract.py", line 262, in extract
    raise ValueError('Unknown extraction method %r' % method)
ValueError: Unknown extraction method 'chameleon'

有人知道,有什么不对吗?

1 个答案:

答案 0 :(得分:2)

对于最新版本的babel和lingua,您的message_extractors配置可能已过时。出于调试目的,could ask for lingua extractors。我实际上不知道如何为babel做这件事。

$ bin/pot-create --list-extractors
chameleon         Chameleon templates (defaults to Python expressions)
python            Python sources
xml               Chameleon templates (defaults to Python expressions)
zcml              Zope Configuration Markup Language (ZCML)
zope              Zope templates (defaults to TALES expressions)

我最近关注pyramid's narrative documentation for i18n/l10。提取工作流程似乎已经改变。使用lingua&gt; = 3.0.9和babel == 1.3我不需要任何setuptools集成配置,如setup.cfg和定义message_extractors的常见情况,如mines&amp;你的。从python&amp;中提取消息字符串变色龙模板开箱即用。但金字塔文档应该有所改进。

几天后I notified the pyramid project about my observations on the topicsuggested a small change to lingua i18n.sh script that helps finding fuzzy messages。也许这些资源可以帮到你。