Table of Contents
multilang-core
Note: The options API is still evolving; expect tweaks as the project grows.
What it does
- Adds Eleventy’s built-in
I18nPluginso locale-aware URLs and filters are available. - Builds a
collections.translationsMapmap keyed bytranslationKeywith per-language entries. - Lets you link alternates (hreflang) and look up localized counterparts in templates.
Defaults
defaultLanguage:"en"if not provided.errorMode:"allow-fallback"(Eleventy I18n option).
Options
defaultLanguage(string, recommended): Primary locale used for fallbacks.languages(array or object, optional): Your language definitions; used to decide if multilingual is on and to pass to sitemap-core. Not passed to Eleventy’s I18n plugin.- Peer deps: none (uses Eleventy’s built-in I18n plugin).
How it works
-
Registers Eleventy’s
I18nPluginwith the provideddefaultLanguageanderrorMode. -
Creates
collections.translations(array) andcollections.translationsMap(map):- Keyed by each page’s
translationKey. translations(array): safe copies of pages that have atranslationKey, each withlocale: { translationKey, lang, isDefaultLang }plus page metadata (url, data, file info).translationsMap(map): each key contains an object keyed by language code:{ title, url, lang, isDefaultLang, data }.langis resolved frompage.data.lang→page.data.language→defaultLanguage.
- Keyed by each page’s
-
Filters available:
i18nTranslationsFor(page, collections.translations)— all siblings for the page’stranslationKey.i18nTranslationIn(page, collections.translations, lang)— specific language variant ornull.i18nDefaultTranslation(page, collections.translations)— default-language variant ornull.
-
Sitemaps: hreflang alternates use
translationsMapto render<xhtml:link>entries. -
You can render alternate links in Nunjucks, e.g.:
{% set t = collections.translationsMap[page.translationKey] or {} %} {% set defaultLang = site.defaultLanguage %} {% set currentLang = page.locale.lang or defaultLang %} {% if t[currentLang] %} <link rel="alternate" hreflang="{{ currentLang }}" href="{{ t[currentLang].url | url }}"> {% endif %} {% for lang, entry in t %} {% if lang != currentLang %} <link rel="alternate" hreflang="{{ lang }}" href="{{ entry.url | url }}"> {% endif %} {% endfor %} {% if t[defaultLang] %} <link rel="alternate" hreflang="x-default" href="{{ t[defaultLang].url | url }}"> {% endif %}
Tips
- Every localized page should set both
translationKeyandlangin front matter. - Keep your default-language page present; it powers the
x-defaultalternate. - Store user-facing labels by language in a map (e.g.,
{ en: "Hello", nl: "Hallo" }) for simple inline translations in Nunjucks. - If
multilingual: trueand alanguagesmap is set,sitemap-corewill emit a sitemap index and per-locale sitemaps. - See the “Multilingual Site” tutorial for a step-by-step setup and hreflang examples.
- Tutorial: Build a Multilingual Baseline Site
Previous: head-core
Next: navigator-core