Search code examples
pythoninternationalizationjinja2pelican

Using the pelican i18n subsites plugin with a static home page


I have a static homepage, but I'm also using the i18n subsites plugin. So for the homepage I have, in pelicanconf.py:

INDEX_SAVE_AS = 'blog/index.html'
INDEX_URL = 'blog'

and for the English version:

I18N_SUBSITES = {
    'en': {
        'OUTPUT_PATH': 'output/en/',
        'INDEX_SAVE_AS': 'blog/index.html',
        'INDEX_URL': 'blog',
    }
}

(truncated unnecessary bits)

The problem lies with the translation link for the homepage. The translations macro has:

{% for translation in article.translations %}
    <a href="{{ SITEURL }}/{{ translation.url }}">{{ translation.lang | lookup_lang_name }}</a>

So for the English-language homepage I could either set the url and output filename as:

    <meta name="save_as" content="en/index.html">
    <meta name="url" content="en/">

Which makes the translation link go to site.com/en/en/ (and works), or set them as:

    <meta name="save_as" content="index.html">
    <meta name="url" content="/">

Which conflicts with the standard-language homepage.

Another, related problem is that the index page (blog page) has no translation link to the English or back to the standard-language version whatsoever.

What can I do to solve this?


Solution

  • I was able to solve my problem with the following translations macro:

    {% macro translations_for(article) %}
    
        {% if extra_siteurls %}
            {% for lang, url in extra_siteurls.items() %}
                {% if article %}
                <a href="{{ url }}/{{article.permalink}}">{{ lang | lookup_lang_name }}</a>
                {% else %}
                <a href="{{ url }}">{{ lang | lookup_lang_name }}</a>
                {% endif %}
            {% endfor %}
        {% endif %}
    
    {% endmacro %}
    

    And by adding a permalink option the each article and page I can also translate the URL for said article or page (otherwise you could just use slug in the above macro).

    Last but not least I also removed the url and save_as data from both homepages.

    To fix the blog page, I added this to the index.html template:

    {% block translation_links %}
        {% if lang == 'nl' %}
        <a href="/en/blog/">English</a>
        {% else %}
        <a href="/blog/">Nederlands</a>
        {% endif %}
    {% endblock %}