如何结合两种机制对鹈鹕网站进行本地化编写?

2024-03-28 14:54:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用两种机制来定位站点: 1我在我的文档中使用标准模板标记{gettext'some\u text'}}索引.html 2我编写了定制的jinja扩展,它根据站点上使用的语言获取markdown文件的内容。你知道吗

我用巴别塔来创造消息.pot文件,然后创建按摩.po文件。你知道吗

我有这个巴别塔的配置巴贝尔.cfg地址:

[jinja2: theme/templates/index.html]
silent = false

这是我的自定义jinja扩展-自定义jinja_扩展.py地址:

from jinja2 import nodes
from jinja2.ext import Extension
from markdown import Markdown


class IncludeMarkdownExtension(Extension):
    """
    a set of names that trigger the extension.
    """
    tags = set(['include_markdown'])

    def __init__(self, environment):
        super(IncludeMarkdownExtension, self).__init__(environment)

    def parse(self, parser):
        tag = parser.stream.__next__()
        ctx_ref = nodes.ContextReference()
        if tag.value == 'include_markdown':
            args = [ctx_ref, parser.parse_expression(), nodes.Const(tag.lineno)]
            body = parser.parse_statements(['name:endinclude_markdown'], drop_needle=True)
            callback = self.call_method('convert', args)
        return nodes.CallBlock(callback, [], [], body).set_lineno(tag.lineno)

    def convert(self, context, tagname, linenum, caller):
        """
        Function for converting markdown to html
        :param tagname: name of converting file
        :return: converting html
        """
        for item in context['extra_siteurls']:
            if item == context['main_lang']:
                input_file = open('content/{}/{}'.format('en', tagname))
            else:
                input_file = open('content/{}/{}'.format(context['main_lang'], tagname))
        text = input_file.read()
        html = Markdown().convert(text)
        return html

我使用这个模板标记-{% include_markdown 'slide3.md' %}{% endinclude_markdown %} 在我的鹈鹕我为jinja扩展添加了这样的字符串:

# Jinja2 extensions
JINJA_ENVIRONMENT = {
    'extensions': [
        'jinja2_markdown.MarkdownExtension',
        'jinja2.ext.i18n',
        'custom_jinja_extension.IncludeMarkdownExtension'
    ]
}

当我运行命令时:

 pybabel extract --mapping babel.cfg --output messages.pot ./

我得到这个错误

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'include_markdown'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.

当我删除所有模板时,使用自定义模板标记gettext效果很好。我做错了什么?


Tags: text标记self模板parserjinja2includehtml
1条回答
网友
1楼 · 发布于 2024-03-28 14:54:35

路上有麻烦。Babel在jinja文件夹中的virtualenv中查找jinja扩展,但我的自定义jinja扩展在项目文件夹中。 所以我在终端上运行这个命令

export PYTHONPATH=$PYTHONPATH:/local/path/to/the/project/ 改变我的生活巴贝尔.cfg地址:

[jinja2: theme/templates/index.html]
extensions=jinja2.ext.i18n, **custom_jinja_extension.IncludeMarkdownExtension**
silent = false

在这个改变之后,巴贝尔找到了我的自定义扩展custom\u jinja\u扩展并创建了消息.pot文件正确!你知道吗

相关问题 更多 >