简体   繁体   English

在金字塔中使用 babel 和 lingua

[英]Using babel and lingua in pyramid

I've problems with babel and lingua.我有 babel 和 lingua 的问题。 I want babel/lingua to scan my source folder for specific strings for creating the pot-catalog.我希望 babel/lingua 扫描我的源文件夹中的特定字符串以创建锅目录。

This is my setup.py这是我的 setup.py

...

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

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

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

And my setup.cfg还有我的 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

In my init .py I have something like this:在我的init .py 我有这样的东西:

config.add_translation_dirs('dbas:locale')

And for example my 404 template is this one:例如我的 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>

Now I can run:现在我可以运行:

python3 setup.py develop
setup.py extract_messages

And I am receiving:我收到:

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'

Does someone know, whats wrong?有人知道吗,怎么了?

Your message_extractors configuration may be outdated for recent versions of babel and lingua.对于最新版本的 babel 和 lingua,您的message_extractors配置可能已经过时。 For debugging purposes you could ask for lingua extractors .出于调试目的,您可以要求 lingua extractors I actually do not know how to do this for babel.我实际上不知道如何为 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)

I recently followed pyramid's narrative documentation for i18n/l10 .我最近关注了金字塔的 i18n/l10 叙述文档 Extraction workflow seems to have changed.提取工作流程似乎发生了变化。 Using lingua >=3.0.9 and babel==1.3 I did not needed any setuptools integration configuration anymore like setup.cfg and defining message_extractors for a common case like mines & yours.使用 lingua >=3.0.9 和 babel==1.3 我不再需要像 setup.cfg 这样的任何 setuptools 集成配置,也不需要为像我和你这样的常见情况定义message_extractors Extraction of message strings from python & chameleon templates worked out-of-the-box.从 python 和变色龙模板中提取消息字符串是开箱即用的。 But pyramid documentation should be improved a bit.但是金字塔文档应该稍微改进一下。

A couple of days later I notified the pyramid project about my observations on the topic and suggested a small change to lingua i18n.sh script that helps finding fuzzy messages .几天后,我将我对该主题的观察通知了金字塔项目,建议对有助于查找模糊消息的 lingua i18n.sh 脚本进行一些小的更改 May be theses resources help you as well.也许这些资源也可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM