Skip to content

Build every language of a site from one mkdocs.yml - #146

Merged
shenxianpeng merged 2 commits into
mainfrom
claude/project-thread-0swvxa
Nov 2, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
claude/project-thread-0swvxa

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Multi-language sites today need one config file per language (mkdocs.zh.yml with INHERIT, its own docs_dir/site_dir, a hand-written extra.alternate) and one mkdocs build per language. This adds native support: one mkdocs.yml declares every language.

languages:
  en: English            # default, built at /
  zh:
    name: 简体中文         # built at /zh/
    site_name: 我的项目     # any other key overrides that setting for this language
    nav:
      - 首页: index.md
      - 指南: guide.md
  • Pages: translations sit next to the original as guide.zh.md, or in a language's own docs_dir (e.g. docs-zh). Links and nav use the plain names in every language. Any file can be translated (img/shot.zh.png).
  • Fallback: a page with no translation is built from the default language's version, so every language has every page. fallback: false leaves them out instead. Images are always shared.
  • Derived settings: theme.locale = language code, site_url and site_dir get /<code>/ added (path: changes the directory).
  • Language switcher: extra.alternate is filled in per page (unless the config sets it), linking to the same page in each language, so Sumi and Material show it without setup. Themes also get a new i18n template variable (current language, fallback flag, per-language link/url/available) for menus and hreflang tags.
  • CLI: mkdocs build builds all languages, mkdocs serve serves and watches all of them; both take --language/-L to work on one.

Plugin compatibility: each language is an ordinary build with its own config, loaded from the same file with that language's overrides, and its own plugin instances. Plugins see a normal single-language site. (Plugins with on_startup are shared across languages, as they are across serve rebuilds.) Unlike mkdocs-static-i18n, nothing reconfigures other plugins. Sites without languages take exactly the old code path.

Two fixes found along the way:

  • Themes without gettext catalogs (Sumi, Material) no longer log "No translations could be found for the locale" for a non-English theme.locale when Babel is installed. It failed --strict builds of every non-English site.
  • The url filter returned the current page for a path above the site root that repeats the page's path (../guide/ on guide/). Now it returns ../../../guide/.

Tested with the built-in themes, Sumi and Material, and with sumi-starter converted to a single config (strict build passes, serve previews both languages).

Related Issue

Roadmap #59 (i18n was planned for 2.x; done now).

Checklist

  • New tests added for new behavior (if applicable)
  • Documentation updated (if applicable): new Multi-language Sites user guide page, languages in the configuration reference, i18n in the theme guide
  • Release notes docs/about/release-notes.md updated (if applicable)

A new `languages` setting lists the languages of a site. The first one is
built at the root of the site and the others into /<code>/. Each language
can override any setting, such as site_name or nav.

Translations sit next to the original page as page.zh.md, or in a
docs_dir of the language's own. A page that has no translation is built
from the default language's version, unless the language sets
`fallback: false`.

Each language is an ordinary build with its own config and its own plugin
instances, so plugins see a normal single-language site. Themes get an
`i18n` template variable, and `extra.alternate` is filled in for each page
so that the language switcher of Sumi and Material links to the same page
in the other languages. `mkdocs build` and `mkdocs serve` take
`--language` to work on one language only.

Also:

- Don't warn about missing gettext catalogs for themes that have none
  (Sumi, Material): the warning failed --strict builds of non-English
  sites when Babel is installed.
- Fix the `url` filter for paths above the site root that repeat part of
  the current page's path, such as ../guide/ on the page guide/.
@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 29 untouched benchmarks


Comparing claude/project-thread-0swvxa (bb3c443) with main (b36f44e)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 25bc85e into main Nov 2, 2026
24 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-0swvxa branch November 2, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant