Add language button and remove all others

This commit is contained in:
Kai Vogelgesang 2025-09-20 01:35:58 +02:00
parent 916c9e60e4
commit 19113626fb
No known key found for this signature in database
GPG Key ID: B51995F56510D27C
3 changed files with 70 additions and 0 deletions

View File

@ -54,6 +54,7 @@ sphinx:
local_extensions: local_extensions:
slide_meta: "extensions" slide_meta: "extensions"
# thebe_local: "extensions" # thebe_local: "extensions"
fix_buttons: "extensions"
config: config:
html_theme_options: html_theme_options:
use_fullscreen_button: false use_fullscreen_button: false

View File

@ -0,0 +1,50 @@
# import json
from pathlib import Path
from sphinx.util import logging
# from docutils import nodes
__version__ = "0.1"
def add_static_sources(app):
static_path = Path(__file__).parent / "static"
app.config.html_static_path.append(static_path.as_posix())
def remove_buttons(app, pagename, _templatename, context, _doctree):
if not pagename.startswith("U0"):
# not one of the units, don't do anything
return
is_engish = pagename.endswith(".en")
buttons = context["header_buttons"]
keep = []
for button in buttons:
try:
if button["tooltip"] == "Start presenting":
keep.append(button)
except Exception as _e:
pass
keep.append({
"type": "javascript",
"javascript": "switchLanguage()",
"tooltip": "Deutsch" if is_engish else "English",
"icon": "fas fa-language",
})
app.add_js_file("switch-language.js")
context["header_buttons"] = keep
def setup(app):
app.connect("builder-inited", add_static_sources)
app.connect("html-page-context", remove_buttons, priority=9999)
return {
"version": __version__,
"parallel_read_safe": True,
"parallel_write_safe": True,
}

View File

@ -0,0 +1,19 @@
const switchLanguage = () => {
// Get current URL
let url = window.location.href;
let path = window.location.pathname;
let filename = path.split("/").pop();
// Detect if it's an English page
if (filename.endsWith(".en.html")) {
// Switch to German: remove ".en"
let newFilename = filename.replace(".en.html", ".html");
let newUrl = url.replace(filename, newFilename);
window.location.href = newUrl;
} else if (filename.endsWith(".html")) {
// Switch to English: add ".en"
let newFilename = filename.replace(".html", ".en.html");
let newUrl = url.replace(filename, newFilename);
window.location.href = newUrl;
}
}