如何结合两种机制来编写鹈鹕上的本地化网站?



我对本地化网站使用两种机制: 1. 我在索引中使用标准模板标签 {{ gettext 'some_text'}}.html 2.我编写了自定义jinja扩展,该扩展根据网站上使用的语言获取markdown文件的内容。

我使用 Babel 创建 messages.pot 文件,然后创建 massages.po 文件。

我在 babel中有这个 babel 配置.cfg :

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

这是我的自定义 jinja 扩展 - custom_jinja_extension.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 %}在我的 pelicanconf.py 中,我为 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.模板语法错误: 遇到未知标记 "include_markdown"。金贾正在寻找以下标签: "端块"。需要关闭的最内层块是"块"。

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

麻烦在路上。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

在此更改之后,babel 找到了我的自定义扩展custom_jinja_extension并正确创建了 messages.pot 文件!

最新更新