MkDocs i18n plugin using static translation markdown files
Project description
MkDocs static i18n plugin
An MkDocs plugin that helps you support multiple language versions of your site / documentation.
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, the 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.
Like what you :eyes:? Give it a :star:!
Language detection logic
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.
- 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
Since demonstrations are better than words, check out the demo documentation here which showcases the logic.
Installation
Just pip install mkdocs-static-i18n!
Configuration
Supported parameters:
- default_language (mandatory): ISO-639-1 (2-letter) string
- languages (mandatory): mapping of ISO-639-1 (2-letter) language code: display value
- material_alternate (default: true): boolean - see this section for more info
plugins:
- i18n:
default_language: en
languages:
en: English
fr: Français
Example output
Using 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
Not building a dedicated version for the default language
If you do not wish to build a dedicated <language>/ path for the default_language version of your documentation, simply do not list it on the languages list. See issue #5 for more information.
The following configuration:
plugins:
- i18n:
default_language: en
languages:
fr: Français
Applied 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
Referencing localized content in your markdown pages
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!
Compatibility with other plugins
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
Adding a language selector on your documentation header
Using mkdocs-material site language selector
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!
If you wish to disable that feature, simply set the material_alternate option to false:
plugins:
- i18n:
default_language: en
languages:
en: English
fr: Français
material_alternate: false
Writing a custom language switcher
Let'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_overrides
Then 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
See it in action!
Contributions welcome!
Feel free to ask questions, enhancements and to contribute to this project!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file mkdocs-static-i18n-0.13.tar.gz.
File metadata
- Download URL: mkdocs-static-i18n-0.13.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
502fb8ca2c8dcc681e0ab12f2e0b2716119a807be2bb2f44ba589ad7dc0689bd
|
|
| MD5 |
9a7e1be94fd49ae4f3cf0bcc8cdb091a
|
|
| BLAKE2b-256 |
206c70e8dd9367d93b47279473faa219d8d833954ad348165a3f00f18548af46
|