Translations

A site can be written in several languages. Each page is translated field by field in index.<lang>.md, and every language is served under its own URL prefix.

Declaring languages

project.language is the primary language: the language of every index.md. Each [alternatives.<code>] table adds another:

[project]
name = "My Site"
language = "en"

[alternatives.en]
name = "English"

[alternatives.de]
name = "Deutsch"

[alternatives.fr]
name = "Français"
url_prefix = "/francais/"

The table name is a language tag such as de or pt-BR (motabhai::config::invalid_language). name defaults to the code. url_prefix defaults to / for the primary language and /<code>/ for the others; it must start and end with / (motabhai::config::invalid_url_prefix), and no two languages may share one (motabhai::config::duplicate_url_prefix).

The primary language does not need a table. Give it one, as en above, to name it for a language switcher or to move it under a prefix of its own. Languages keep the order of the file, with the primary one first.

Translating a page

Every page exists in every language. In another language it reads index.<code>.md beside index.md, if there is one:

content/about/
  index.md        # English
  index.de.md     # German
+++
title = "Über uns"
+++

Wir sind …

A translation overrides the primary page field by field. Each field it sets replaces the primary's; every other field falls back to index.md. A non-empty body replaces the primary's body. A page with no file for a language is the primary page in that language. So a German page can translate just the title and body and keep the dates, tags and images of the English one.

Rules for translations:

  • A translation cannot set _model (motabhai::content::alternative_model), and has no required fields of its own. The fields it does set are checked against the model like any content.
  • _slug, _template, _hidden, _discoverable, _created and _redirect_from may differ per language. Each falls back to what the primary page set, except _redirect_from: a translation redirects only from the URLs it lists itself.
  • A translation with _hidden = true hides the page in that language only.
  • A file for a language the config does not have, or for the primary language, is motabhai::content::unknown_alternative.
  • Attachments are shared: they are copied beside the page in every language, so relative links work in all of them.
  • A slug_format runs on each language's own fields, so translated titles give translated URLs.
  • A flow field in a translation replaces the primary's blocks as a whole.
  • A translation of a notebook page (index.ipynb) is a notebook too, index.<code>.ipynb, and one of a Markdown page is Markdown. The other format is motabhai::content::translation_format_mismatch. A notebook translation's fields, in metadata.motabhai, fall back like any other; its cells are its own body and do not fall back.

URLs

Each language is served under its prefix, below the base path:

PageEnglishGerman
///de/
/about/about//de/about/

Each language's pages form their own tree, so children, prev, next and every query stay within one language.

Templates

Templates see the language of the page being rendered:

  • this._alt is the page's language code.
  • site.get, site.query and site.root find pages in the page's language. site.get("/about", "en") reaches another one; a language the site does not have is motabhai::template::invalid_query.
  • "/about"|url, and links in Markdown, lead to the page in the same language: /de/about/ on a German page.
  • config.alternatives lists every language with code, name, url_prefix and primary.

this.alternatives builds a language switcher. It lists the page in each language it is not hidden in, in config order, each with alt (the code), name, record, url_path and is_current:

<nav class="languages">
  {% for alt in this.alternatives %}
  <a href="{{ alt|url }}" hreflang="{{ alt.alt }}"
     {%- if alt.is_current %} aria-current="page"{% endif %}>{{ alt.name }}</a>
  {% endfor %}
</nav>

Use this._alt for the lang attribute, since config.language is always the primary language:

<html lang="{{ this._alt }}">

Building

Every language gets its own pages, feeds and tag pages. The sitemap lists every language and links each page to its translations with xhtml:link alternates. Editing index.de.md rebuilds only the German page; editing index.md rebuilds every language that may fall back to it.

motabhai check counts pages once and says how many languages there are. The admin UI edits translations too, saving them to index.<code>.md.

Limits

  • The 404 page is built for the primary language only.
  • A tag page's language switcher links to the parent page in each language, and a paginated page's to page 1.