Post

How to translate permalinks with Jekyll Polyglot

To install the jekyll-polyglot plugin - add the following to your Gemfile:

1
2
3
group :jekyll_plugins do
  gem "jekyll-polyglot"
end

Install the plugin: bundle install.

Add name attribute to each locale file inside _data/locales/. This name will be displayed in switcher.

Create lang-switcher.html in _includes:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- language switcher -->
<hr>
<ul style="list-style-type: none">
{% for lang in site.languages %}
  <li>
    <i class="fa-regular fa-flag" style="margin: 5px"></i> 
    <a class="lang-name" {% static_href %}href="{{ site.baseurl }}{% if lang != site.default_lang %}/{{lang}}{% endif %}{{ page.permalink_lang[lang] | default: '/' }}"{% endstatic_href %}>
      {{ site.data.locales[lang].name }}
    </a>
  </li>
{% endfor %}
</ul>

The page object has a rarely mentioned, but helpful permalink_lang attribute.

Include it somewhere (e.g. in sidebar.html):

1
{% include lang-switcher.html %}

And voila! Remember that your pages and posts should have page_id and permalink attributes.

1
2
3
4
5
6
7
8
---
layout: page
icon: fas fa-info-circle
order: 4
title: About
page_id: about
permalink: /about/
---
This post is licensed under CC BY 4.0 .