-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom language selector (non-mkdocs-material) #297
Comments
Hello @tombreit I think you can take inspiration from : https://github.com/ultrabug/mkdocs-static-i18n/blob/main/mkdocs_static_i18n/custom_i18n_sitemap/sitemap.xml |
Hi @ultrabug, thanks for your prompt reply and the sitemap-hint! But in my case, there must be something wrong with my project setup - even
# mkdocs.yml
site_name: Testsite
strict: true
plugins:
- i18n:
docs_structure: suffix
languages:
- locale: en
default: true
name: English
build: true
- locale: de
name: Deutsch
build: true
theme:
name: null
custom_dir: 'theme/' # project layout
.
├── docs
│ ├── page1.de.md
│ └── page1.en.md
├── mkdocs.yml
├── theme
│ ├── base.html
│ ├── main.html
│ └── mkdocs_theme.yml
└── upload.sh <!-- theme/base.html -->
i18n_page_locale: {{ i18n_page_locale }}
i18n_languages: {{ i18n_languages }}
i18n_alternates: {{ i18n_alternates.items() }} → File "project/theme_csl/main.html", line 1, in top-level template code
{% extends "base.html" %}
File "project/theme_csl/base.html", line 86, in top-level template code
{{ i18n_alternates.items() }}
^^^^^^^^^^^^^^^^^^^^^^^^^
File "project/.venv/lib/python3.11/site-packages/jinja2/environment.py", line 485, in getattr
return getattr(obj, attribute)
^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'i18n_alternates' is undefined Do you have any other tips or ideas? |
The
For sitemap.xml and perhaps 404.html as well, as it's included in static_templates. The page context has only those values: mkdocs-static-i18n/mkdocs_static_i18n/plugin.py Lines 182 to 188 in 0b492e1
|
Based on this excerpt from the material theme, the templates can access the So try: {% if "i18n" in config.plugins %}
{% set i18n_alternates = config.plugins["i18n"].i18n_files_per_language %}
{% else %}
{% set i18n_alternates = {} %}
{% endif %}
{{ i18n_alternates.items() }}
I haven't tested it ✌️ |
Sorry, but I don't get it. You put me on the right track - but <!-- mytheme/base.html -->
{% for locale, i18n_files in config.plugins["i18n"].i18n_files_per_language.items() %}
- locale: {{ locale }}
i18n_files: {{ i18n_files }}
{% endfor %} only works for my secondary language pages - it does not list alternate
Hard to explain, so I have put together a demo page: |
Didn't run the demo, your comment explained the situation quite well: mkdocs-static-i18n/mkdocs_static_i18n/plugin.py Lines 76 to 78 in 0b492e1
It turned out that the i18n_files_per_language is a mapping that gets updated after each language build, and the sitemap uses the data from the last build to save the information, once all languages are built.
Going back to your OP:
As far as I know, the i18n plugin requires or assumes that the file tree is the same in each language, so every file path is the same too only the Given this information you don't need to access the files from other languages, you only need the language identifier.
mkdocs-static-i18n/mkdocs_static_i18n/reconfigure.py Lines 110 to 114 in 0b492e1
You should be able to access the data with:
accessing |
That would work if all pages exists in all configured languages. Looping over But neither Given the following docs/
├── enonly.en.md
├── index.de.md
└── index.en.md this would work for Perhaps I implement this language switcher in Javascript;-) |
Well, both
So that after a page load, the JavaScript fetches the urls to check if the page is available and returns a status 200? I advise against this, as this would just create unnecessary requests. This is the code that reconfigures the context and configures the alternates: mkdocs-static-i18n/mkdocs_static_i18n/reconfigure.py Lines 424 to 442 in 0b492e1
There is the page.file.alternates list, but I don't think it will limit the alternates to only existent ones, still worth a try.
Also the Material theme uses the |
I already tried to hijack the language switcher from mkdocs-material. But, again, I'm unable to use the provided "integration" via mkdocs-static-i18n/mkdocs_static_i18n/reconfigure.py Lines 126 to 127 in 0b492e1
This integration actually sounds perfect and I would love to use it - but the static-i18n plugin only seems to provide/expose this for "mkdocs-material"? |
True, my bad, I forgot about the The code responsible for the alternate deepcopy could be moved out into another function mkdocs-static-i18n/mkdocs_static_i18n/reconfigure.py Lines 306 to 356 in 0b492e1
and later it can be called inside the reconfigure_material_theme like before, and in an elif "alternate" in config.extra statement after that whole material validation:
I think it's a valid proposition to support that feature in other themes, one issue might be that other themes use IMO that logic is not that coupled with material, that it can't be taken out 🤔However, I won't be developing anything for at least a week+ so can't make a PR myself. Once the market research is done ping ultrabug, maybe he'll have the time if you don't want to dabble with Python directly. |
I would like to build a language selector in a custom (non-mkdocs-material) theme, but I do not know how to access the alternate language versions of a given page:
<!-- theme/base.html --> i18n_page_locale: {{ i18n_page_locale }} i18n_languages: {{ i18n_languages }}
i18n_page_locale
gives me the expected language code for the current language version.But the variable
i18n_languages
seem to be undefined.How do I access the alternate language versions of a given page in a custom template?
The text was updated successfully, but these errors were encountered: