How to combine two mechanism for localization site written on Pelican?

167 views Asked by At

I use two mechanisms for localization site: 1. I use the standard template tag {{ gettext 'some_text'}} in my index.html 2. I wrote custom jinja extension that takes the content of markdown file according to language that used on the site.

And I use Babel to create messages.pot file and then to create massages.po file.

I have this babel configuration in babel.cfg :

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

And this is my custom jinja extension - 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

I use this template tag - {% include_markdown 'slide3.md' %}{% endinclude_markdown %} In my pelicanconf.py I add such strings for jinja extensions:

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

When I run the command:

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

I get this error

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'.

When I delete all using of custom template tag gettext work well. What I do wrong?

1

There are 1 answers

0
Анастасия Руденко On

Trouble was in the path. Babel looking jinja extension in virtualenv in jinja folder, but my custom jinja extension was in the project folder. That's why I run this command in the terminal

export PYTHONPATH=$PYTHONPATH:/local/path/to/the/project/ and change my babel.cfg :

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

After this changes babel found my custom extension custom_jinja_extension and created messages.pot file correctly!