Bernhard Scheirle


About Me

Hello, my name is Bernhard.
I'm a computer science student at Karlsruher Institute of Technology.

Contact Me



blogroll


Pelican – Improved Display of Translations


This article is about Pelican1, a static site generator, and about customising it's theme.

A few days ago I received a e-mail asking me about the theme of this blog and especially how it handles translations of articles:

If there are no translations:

Image: No translation

If there are translations:

Image: Translation

Pelican it self already supports translations and a simple method to select the language:

1
2
3
4
<!-- Jinja Theme -->
{% for translation in article.translations %}
    <a href="{{ SITEURL }}/{{ translation.url }}">{{ translation.lang }}</a>
{% endfor %}

The problem with this snippet is that it only outputs country codes (en, de, …) instead of full language names. To fix this we need a way to convert country codes to language names. Therefor I'm using a custom Jinja-Filter:

pelicanconf.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
language_lookup = {
    'en': 'English',
    'de': 'Deutsch',
}

def lookup_lang_name(lang_code):
    return language_lookup[lang_code]

JINJA_FILTERS = {
    'lookup_lang_name': lookup_lang_name,
}

Theme:

Now lets use this filter and some additional "fluff" to enhance the output.

1
2
3
4
5
6
7
8
{{ article.lang | lookup_lang_name }},
{% if article.translations %}
    {% for translation in article.translations %}
        <a href="{{ SITEURL }}/{{ translation.url }}">{{ translation.lang | lookup_lang_name }}</a>{% if not loop.last %}, {% endif %}
    {% endfor %}
{% else %}
    No translations
{% endif %}

Changes:

  • Output the current languge (Line 1).
  • Add commas between languages (Line 4).
  • If there are no translation output this as well. (If-Statement)
  • Use our custom Jinja-Filter

And now you have a nice looking display to change the language.


  1. Pelican is a static site generator, written in Python, that requires no database or server-side logic. http://blog.getpelican.com/ 

Comments


There are no comments yet. Why aren't you the FIRST and shout something?

Add a Comment

You may format you comment with Markdown.

Comment Atom Feed