pelicanconf.py - Localisation

Additional DateTime Informations in Meta Data of Pages and Articles

As I want to retrofit some of my older "footprints" in the WWW into my blog I want to add additional dates to the meta data of pages and articles. To have these properly localized the string meta data must be converted to datetime objects.

import datetime
def germandatestring2string_filter(germandatestring):
    return datetime.datetime.strptime(germandatestring,'%d.%m.%Y')

My entries have to be consistently in the german date format.

Translation of Category Names

CATEGORY_TRANSLATIONS = { 'conferences' : { 'de': 'Konferenzen', 'en': 'Conferences', },
                            'homepage' : { 'de': 'Heimatseite', 'en': 'Home Page', }, }

def localizecategory_filter (lang,category):
    return CATEGORY_TRANSLATIONS.get(category).get(lang)

Translation of Phrases in Templates

In some templates text fragments are indispensable and shall be localized.

PHRASE_TRANSLATIONS = {
        'imprint' : { 'de': 'Impressum', 'en': 'Imprint' },
        'landingpage_blogheader' : { 'de': 'Neuste Posts', 'en': 'Recent Posts' },
        'createdwith' : { 'de': 'Erstellt mit', 'en': 'Created with' },
        'and' : { 'de': 'und', 'en': 'and' },
        'bloggedby' : { 'de': 'Von', 'en': 'By'},
        'bloggedon' : { 'de': 'gebloggt am', 'en': 'blogged on' },
        'bloggedin' : { 'de': 'In', 'en': 'In' },
        'translations' : { 'de': 'Übersetzungen', 'en': 'Translations' }  }

def localizephrase_filter (lang,phrase):
    return PHRASE_TRANSLATIONS.get(phrase).get(lang)

Finally the filters are registered.

JINJA_FILTERS = {
    'germandatestring2date': germandatestring2string_filter,
    'localizecategory': localizecategory_filter,
    'localizephrase': localizephrase_filter
    }

Usage

{% if article.blogdate %}
({{ article.lang | localizephrase('bloggedon') }} {{ article.blogdate | germandatestring2date | strftime(article.date_format) }})
{% endif %}<br/>
{{ article.lang | localizephrase('bloggedin') }}
<a href="{{ ... }}/{{ article.category }}.html">{{ article.lang | localizecategory(article.category) }}</a>.<br/>

Copyright © 2016 Uwe Ritzmann - Created with Pelican, Python and Skeleton.