An MkDocs plugin that helps you support multiple language versions of your site / documentation.
Like what you π? Using this plugin? Give it a β!
The mkdocs-static-i18n plugin allows you to support multiple languages of your documentation by adding static translation files to your existing documentation pages.
Multi language support is just one .<language>.md file away!
Even better, mkdocs-static-i18n also allows you to build and serve localized versions of any file extension to display localized images, medias and assets.
Localized images/medias/assets are just one .<language>.<extension> file away!
If you want to see how it looks, check out the demo documentation here.
This plugin is made to be as simple as possible and will generate a default version of your website + one version per configured language on the <language>/ path.
Your translations can be detected using a suffix based docs structure (default) or a folder based docs structure. This is controlled by the docs_structure option.
Since demonstrations are better than words, check out the demo documentation here which showcases the logic.
The default behavior is to use a suffix based docs structure to handle your translations:
- the
defaultversion will use any.mddocumentation file first and fallback to any.<default_language>.mdfile found - the
/<language>language versions will use any.<language>.mddocumentation file first and fallback to any.<default_language>.mdfile before fallbacking to any default.mdfile found
The optional behavior is to use a folder based structure to handle your translations:
- each of your configured language should have a folder with their name at the root of
docs_dir, which of course includes the default language of your choice - then you just structurate your
.mddocumentation files in their respective folder structure - any folder relative to
docs_dirwhich is not a language folder will be copied as-is and their files will serve as language agnostic (think of non localized images)
See a using a folder per language structure section for more details.
Just pip install mkdocs-static-i18n!
Here is the list of all the options that the plugin supports.
- mandatory:
yes
A 2-letter ISO-639-1 language code (en) or 5-letter language code with added territory/region/country (en_US).
default_language: en- default:
false
This boolean (true/false) option is used to speed up the build process while developing your documentation.
default_language_only: false- default:
suffix
This (fixed) string (can be either 'suffix' or 'folder') option is used to select which language detection logic should be used to build the localized versions of your documentation.
docs_structure: folder- mandatory:
yes
A mapping of 2-letter or 5-letter language code (like default_language) with either the display name of the language or some optional keys used to control the build process of each language.
Allowed options per language:
name(mandatory): the name that should be displayed in the mkdocs-material language switcherlink(default:./<language>): the link path to be used in the mkdocs-material language switcherfixed_link(default:None): if specified, when switching to this language, you will be redirected to this linkbuild(default:true): a boolean used to control the build of a/<language>path for the given languagesite_name(default:mkdocs.yml site_name): thesite_nametranslation for the given language
Feature rich languages options usage:
plugins:
- i18n:
default_language: en
languages:
en:
name: English
build: true
fr:
name: FranΓ§ais
build: trueShort (legacy) languages options usage:
plugins:
- i18n:
default_language: en
languages:
en: English
fr: FranΓ§ais- default:
true
If you're using the mkdocs-material theme, this boolean (true/false) option will let you choose if you want the mkdocs-static-i18n plugin to configure the language switcher for you..
material_alternate: true- default:
empty
This option is a nested mapping of language: default title: translated title that allows you to translate the navigation sections of your website.
nav_translations:
fr:
Topic1: Sujet1
Topic2: Sujet2- default:
true
This is a boolean (true/false) option that lets you activate or not the automatic reconfiguration of the plugins.search.lang option based on your configured languages.
plugins:
- i18n:
default_language: en
languages:
en:
name: English
build: true
fr:
name: FranΓ§ais
build: trueUsing the configuration above on the following docs/ structure will build the following site/ (leaving out static files for readability):
docs
βββ image.en.png
βββ image.fr.png
βββ index.fr.md
βββ index.md
βββ topic1
β βββ index.en.md
β βββ index.fr.md
βββ topic2
βββ index.en.md
βββ index.md
site
βββ en
β βββ image.png
β βββ index.html
β βββ topic1
β β βββ index.html
β βββ topic2
β βββ index.html
βββ fr
β βββ image.png
β βββ index.html
β βββ topic1
β β βββ index.html
β βββ topic2
β βββ index.html
βββ image.png
βββ index.html
βββ topic1
β βββ index.html
βββ topic2
βββ index.html
The mkdocs-static-i18n plugin since version 0.32 provides a template that will generate automatically an alternate aware sitemap.xml so that your localized content is made available to search engines!
Localization aware sitemap.xml:
<url>
<loc>https://ultrabug.github.io/mkdocs-static-i18n/</loc>
<lastmod>2022-01-31</lastmod>
<changefreq>daily</changefreq>
<xhtml:link rel="alternate" hreflang="en" href="https://ultrabug.github.io/mkdocs-static-i18n/en/"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://ultrabug.github.io/mkdocs-static-i18n/fr/"/>
</url>If you do not wish to build a dedicated <language>/ path for the default_language version of your documentation, just set its build option to false.
The following configuration:
plugins:
- i18n:
default_language: en
languages:
fr:
name: FranΓ§ais
build: true
en:
name: English
build: falseApplied on the following structure:
docs
βββ image.en.png
βββ image.fr.png
βββ index.fr.md
βββ index.md
βββ topic1
β βββ index.en.md
β βββ index.fr.md
βββ topic2
βββ index.en.md
βββ index.md
Will build:
site
βββ fr
β βββ image.png
β βββ index.html
β βββ topic1
β β βββ index.html
β βββ topic2
β βββ index.html
βββ image.png
βββ index.html
βββ topic1
β βββ index.html
βββ topic2
βββ index.html
When working on your project, building a version for each supported language can be slow depending on the size of your documentation.
The default_language_only option allows you to only build the selected
default_language of your documentation for faster development.
Coupled with MkDocs >= 1.2 support for environment variables,
this option can easily be passed dynamically within your mkdocs.yml file like this!
You can read more about the rationale behind this feature here.
Focus on translating your content, not on updating all the links and references to your assets!
Let mkdocs-static-i18n do the heavy lifting of dynamically localizing your
assets and just reference everything without their localized extension.
Since the generated site files have their localization extension removed
during the build process, you can reference them in your markdown source
without it (this includes links to .md files)!
This simple docs structure:
docs
βββ image.en.png
βββ image.fr.png
βββ index.fr.md
βββ index.md
Will generate this site tree:
site
βββ fr
β βββ image.png
β βββ index.html
βββ image.png
βββ index.html
Which means that the image.png and its fr/image.png localized counterpart
can be referenced the same way as  on both index.md
and index.fr.md!
Using the site_name option for a given language, you can translate the site
name that will be used to render the localized pages for the language version
of your website.
site_name: Default site title
plugins:
- i18n:
default_language: en
languages:
en:
name: English
site_name: Site title in English for pages with path /en/
fr:
name: FranΓ§ais
site_name: Titre du site en FranΓ§ais pour les pages en /fr/Using the nav_translations configuration option, you can translate all your
navigation titles easily.
Translations are applied to all titles so you only need to provide a given translation once and it will be used to translate all the sections, links and pages which share the same title.
This example will translate any navigation item title from Topic1 to Sujet1 on the French version of the documentation:
plugins:
- i18n:
default_language: en
languages:
en:
name: English
fr:
name: FranΓ§ais
nav_translations:
fr:
Topic1: Sujet1
Topic2: Sujet2Since version 0.20 of the plugin, localized content can diverge from the default language version. This means that you can have pages that are specific to some languages without any problem.
See #59 if you need more information about this.
If you enabled the search plugin embedded with MkDocs, this plugin will
automatically populate its lang option with the the configured languages
as long as they are supported by lunr.
This means that your search results can't be contextual to the language you are currently browsing.
The mkdocs-static-i18n plugin will try to be smart and deduplicate the pages from the default_language so that search results are not polluted by redundant content.
This is because the MkDocs search plugin is hardcoded in the themes
javascript sources so there can only be one search index for the whole
build.
This feature can be disabled using the search_reconfigure option.
This plugin is compatible with the following mkdocs plugins:
- MkDocs Material: the
searchplugin text will be switched automatically to the right language depending on the version you're browsing and thelanguage selectorwill automatically be setup for you (requires mkdocs-material>=7.1.0) - MkDocs Awesome Pages Plugin: the page ordering is preserved on the language specific versions of your site
- MkDocs with PDF Plugin: you get a PDF of every localized version of your site
- MkDocs minify Plugin: to minify your JS, CSS and HTML localized files
Starting version 7.1.0, mkdocs-material supports a site language selector configuration.
The mkdocs-static-i18n plugin will detect if you're using mkdocs-material and, if its version is at least 7.1.0, will enable and configure the site language selector automatically for you unless you specified your own extra.alternate configuration!
Even better, mkdocs-static-i18n will also make it so that changing between languages keeps you on the same page instead of getting you back to the language specific home page (not compatible with theme.features = navigation.instant, see #62)!
If you wish to disable that feature, simply set the material_alternate option to false:
plugins:
- i18n:
default_language: en
languages:
en:
name: English
fr:
name: FranΓ§ais
material_alternate: falseLet's take mkdocs-material as an example and say we would like to add two buttons to allow our visitors to switch to their preferred language.
The following explanation was showcased in the demo website up to 0.7 so you can find the files here:
We need to add a custom_dir to our theme configuration:
theme:
name: material
custom_dir: theme_overridesThen override the header.html to insert something like:
{% if "i18n" in config.plugins %}
<div style="margin-left: 10px; margin-right: 10px;">
{% include "partials/i18n_languages.html" %}
</div>
{% endif %}
And add a i18n_languages.html that could look like this:
{% for lang, display in config.plugins.i18n.config.languages.items() -%}
<div style="display: inline-block; margin-right:5px;"><a href="/{{ lang }}/">{{ display }}</a></div>
{% endfor %}
The resulting files should be placed like this:
theme_overrides
βββ partials
βββ header.html
βββ i18n_languages.html
The plugin exports some useful i18n variables that you can access through the page context:
i18n_config: the i18n plugin configurationi18n_page_locale: the current rendering locale of the pagei18n_page_file_locale: the locale suffix of the source file used to render the page
Those context variables can be accessed using Jinja2 notation, like {{ i18n_page_locale }} in your theme overrides.
docs_dir layout so make sure you respect it properly.
The mkdocs-static-i18n plugin can be configured to work with a language per folder based structure where you create a folder per language your want to support translations for.
For example, the given folder structure:
docs_folder_structure
βββ assets
β βββ image_non_localized.png
βββ en
β βββ image.png
β βββ index.md
β βββ topic1
β β βββ named_file.md
β βββ topic2
β βββ README.md
βββ fr
βββ image.png
βββ index.md
βββ topic1
β βββ named_file.md
βββ topic2
βββ README.md
With the following mkdocs.yml configuration:
plugins:
- i18n:
default_language: en
docs_structure: folder
languages:
en:
name: English
fr:
name: FranΓ§aisWill build:
site
βββ assets
β βββ image_non_localized.png
βββ en
β βββ image.png
β βββ index.html
β βββ topic1
β β βββ index.html
β βββ topic2
β βββ index.html
βββ fr
β βββ image.png
β βββ index.html
β βββ topic1
β β βββ index.html
β βββ topic2
β βββ index.html
βββ image.png
βββ index.html
βββ topic1
β βββ index.html
βββ topic2
βββ index.html
Feel free to ask questions, enhancements and to contribute to this project!