Skip to content

Translations and languages

Aleix Batalla edited this page Feb 6, 2017 · 15 revisions

Where are stored the translations?

Translations are stored at _data/translations.yml with the following structure:

slogan:

  ca: El proper gran esdeveniment Drupal a Barcelona

  en: Next big Drupal event in Barcelona

  es: El próximo gran evento Drupal en Barcelona

learn_more:

  ca: descobreix més

  en: discover more

  es: descubre más

How to load translations?

To load a translation in any place it is needed to use:

{{ site.data.translations.KEY["LANGUAGE"] }}

For instance, to load catalan version of slogan you need to use

{{ site.data.translations.slogan["ca"] }}.

In order to write less code the variable T is defined in the main layouts (landing.html and default.html) like:

{% assign T = site.data.translations %}

This definition lets us load translations as follows:

{{ T.slogan["ca"] }}

or using the current_language

{{ T.slogan[current_language] }}

variable or page.language variable

{{ T.slogan[page.language] }}

Who sets or defines current_language?

get-current-language.html in _includes directoy sets current_language. It parses the url and uses the first element after the base url to detect the language of the site. This element is checked agains site.enabled_languages (from _config.yml). If the element is not in site.enabled_languages, site default language is provided.

What's the site default language?

In _config.yml is defined site.enabled_languages. For instance:

enabled_languages: ["ca", "en", "es"]

The first array item ("ca") is the default language.