How to generate links for all languages on top in Pelican site for current page (article)
Asked Answered
M

1

6

Hi i'm using Pelican/Python for building a small static multilanguage site.

With i18n_subsites plugin I can add language buttons that shows other available languages on top of my site.

Is there any way to specify links on these language buttons to current page (article) translation? Not for main page? It will be good to stay on the same page (article).

Any help would be greatly appreciated.

Mulhouse answered 28/8, 2014 at 9:4 Comment(0)
M
4

May be it will be useful for someone else.

This is tempating issue. Many thanks to smartass101

You could do something like this:

{% if lang_siteurls %}
{% for lang, url in lang_siteurls.items() %}
<li{% if lang == DEFAULT_LANG %} class="active"{% endif %}>
<a href={% if article and lang != DEFAULT_LANG %}"{{ SITEURL }}/{{ article | extract_trans(lang, url) }}"
     {% elif article %}"{{ SITEURL }}/{{ article.url }}"
     {% elif page and lang != DEFAULT_LANG %}"{{ SITEURL }}/{{ page | extract_trans(lang, url) }}"
     {% elif page %}"{{ SITEURL }}/{{ page.url }}"
     {% else %}"{{ url }}"{% endif %}>{{ lang }}</a></li>
{% endfor %}
<!-- separator -->
<li style="background-color: white; padding: 5px;">&nbsp</li>
{% endif %}

where the filter is defined as

def extract_trans(article, lang, url):
    for trans in article.translations:
        if trans.lang == lang:
            return trans.url
    return url

and included using the JINJA_FILTERS setting. This code works for me for all pages and articles with exception of archives.

Mulhouse answered 28/8, 2014 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.