Multilingual sites
Tola stores your configured languages (site.language and site.languages) without enforcing a directory layout or URL convention. Separating the locale prefix from each page’s logical key makes translations, fallbacks, and language switchers straightforward.
Locale layout
Declare all BCP 47 language tags in tola.toml (language must appear inside languages):
[site]
title = "My Site"
origin = "https://example.com"
language = "en"
languages = ["en", "zh-Hans"]Group sources by language directory while keeping identical relative paths for translated pages:
content/
en/
index.typ
docs/install.typ
zh-hans/
index.typ
docs/install.typIn your entry, determine the locale from the leading directory segment and store the remaining path as a language-neutral logical key (such as /docs/install/):
- Either omit the prefix for the default language (
/docs/install/vs/zh-hans/docs/install/) or keep explicit prefixes for every locale (/en/docs/install/and/zh-hans/docs/install/). - Only
page.outputchanges between the two schemes;page.keystays identical.
Text & messages
Each entry in site.languages provides parsed fields: tag (e.g. "zh-Hans"), lang, script, and region. Apply them to both Typst text rules and the HTML lang attribute:
#set text(
lang: page.locale.lang,
script: page.locale.script,
region: page.locale.region,
)
#document(page.output, format: "html", title: page.meta.title)[
#html.html(lang: page.locale.tag)[
...
]
]Keep UI strings (“Previous”, “On this page”, “Search”) in per-locale dictionary modules (such as locales/en.typ and locales/zh-Hans.typ) and read page.locale.messages from components.
Language switcher
Group all translations by page.key and also build a lookup by (locale.tag, page.key). If a target locale does not have a translation for the current page, walk up to the nearest published parent route instead of emitting a broken link or omitting the option:
#import "@tola/address:0.0.0": join-route, output-url
#import "@tola/collection:0.0.0": group-by, index-by
#let translations-by-key = group-by(pages, key: p => p.key)
#let by-lang-and-key = (:)
#for (tag, items) in group-by(pages, key: p => p.locale.tag) {
by-lang-and-key.insert(tag, index-by(items, key: p => p.key))
}
#let resolve-language-target(page, target-tag) = {
let table = by-lang-and-key.at(target-tag, default: (:))
if page.key in table { return table.at(page.key) }
// Walk upward to the closest published ancestor
for depth in range(page.segments.len() - 1, -1, step: -1) {
let fallback-key = join-route(page.segments.slice(0, depth))
if fallback-key in table { return table.at(fallback-key) }
}
none
}
#let language-menu(page, locale-tags) = {
html.nav(aria-label: "Languages")[
#for tag in locale-tags {
let target = resolve-language-target(page, tag)
if target == none { continue }
let attrs = (href: output-url(target.output), lang: tag, hreflang: tag)
if tag == page.locale.tag { attrs.insert("aria-current", "page") }
html.a(..attrs, tag)
}
]
}Localizing body links
Hardcoding /en/docs/install/ or /zh-hans/docs/install/ inside translated prose breaks easily when copying drafts. Let authors write prefix-free logical paths like #link("/docs/install/")[Install] and resolve them per page language in a show link rule:
#import "@tola/site:0.0.0": site
#let localize-link(el, current-lang-targets, translations) = {
if type(el.dest) != str or not el.dest.starts-with("/") or el.dest.starts-with("//") {
return el
}
if el.dest.contains(regex("[%?#]")) { return el }
let key = if el.dest.ends-with("/") { el.dest } else { el.dest + "/" }
if key not in translations { return el }
// Prefer current language; fall back to default site language
let target = current-lang-targets.at(key, default: none)
if target == none {
let pool = translations.at(key)
target = pool.find(p => p.locale.tag == site.language.tag)
if target == none { target = pool.first() }
}
link(target.label, el.body)
}hreflang
When [site].origin is set, emit <link rel="alternate" hreflang="..."> entries in <head> for every published translation, plus x-default pointing to the default locale:
#let hreflang-links(translations) = {
let tags = translations.map(t => html.link(
rel: "alternate",
hreflang: t.locale.tag,
href: output-url(t.output, origin: site.origin),
))
let default-page = translations.find(t => t.locale.tag == site.language.tag)
if default-page != none {
tags.push(html.link(
rel: "alternate",
hreflang: "x-default",
href: output-url(default-page.output, origin: site.origin),
))
}
tags
}Emit separate RSS/Atom feeds per locale (such as /en/feed.xml and /zh-hans/feed.xml) and reference each locale’s feed from its <head>.