Skip to content

Language Name Liquid Tag

Vincent Wochnik edited this page Nov 18, 2015 · 1 revision

As a special liquid tag, the plugin includes tl which prints the label of a specific language in the translation of the language that is currently being rendered.

Syntax

{% tl language_name_expression %}

Arguments

The first and only argument, language_name_expression, is an expression evaluating to the language name string of the language for which the label is being printed. This can be a quoted string directly evaluating to the key or a liquid variable whose contents evaluate to the language key.

Translation language labels

This liquid filter requires a lang subset for each language that it is being used with. This specific subset contains labels for each language that this liquid tag is being used for.

Here is the lang subset for the English and German languages which can be complemented by other languages as well:

---
en:
  lang:
    en: English
    de: German
de:
  lang:
    en: Englisch
    de: Deutsch

Example

Print a label for the English language

This is very straight forward:

{% tl 'en' %}

Offer the user a selection of the available languages

Since the plugin does not know which languages it is supporting, an array of languages needs to be added to the configuration _config.yml:

languages: ['en', 'de']

With this array, we can iterate over all supported languages and print out links with labels allowing the user to switch between languages easily:

<ul>
{% for language in site.languages %}
  <li>
    {% if page.language == language %}
      <a class="active" title="{% tl language %}">{% tl language %}</a>
    {% else %}
      <a href="{{ site.baseurl }}/{{ language }}/" title="{% tl language %}">{% tl language %}</a>
    {% endif %}
  </li>
{% endfor %}
</ul>