untra / polyglot

:abc: Multilingual and i18n support tool for Jekyll Blogs
https://polyglot.untra.io
MIT License
394 stars 58 forks source link

[permalink] Ensure that the permalink is correctly set #199 #200

Open hacketiwack opened 2 months ago

hacketiwack commented 2 months ago

It ensures that the permalink_lang attribute is set correctly and can be used when a permalink attribute is set at the site level. It is, indeed, not enough to check if the permalink is undefined. The permalink might be set to empty. Moreover, we should check that the page has a lang attribute isn't empty as well.

🔤 Polyglot PR

In cases where no permalink is set in the post or page, but there is a permalink set at the site level, this fix ensures that the permalink_lang attribute is set correctly.

Then, a language switch can be simply implemented, example below:

<div class="language">
  {% for language in site.languages %}
    {% if language != site.default_lang %}
      {% assign url = language | append: page.permalink_lang[language] %}
    {% else %}
      {% assign url = page.permalink_lang[language] %}
    {% endif %}
    <a class="nav-link" {% static_href %} href="{{url | relative_url}}" {% endstatic_href %}>
      {{ site.data.translations.languages[language] }}
    </a>
  {% endfor %}
</div>

Note: I didn't create a test case, because I'm not familiar with Ruby and the testing mechanism. 😢

Type of change

Checklists