Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 911
Squirrell Avatar asked Aug 28 '14 09:08

Squirrell


1 Answers

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.

like image 142
Squirrell Avatar answered Oct 21 '22 03:10

Squirrell