From 9fbb30a5c64e751cec0fc7fef8a94f7d581eab02 Mon Sep 17 00:00:00 2001 From: tlopex <820958424@qq.com> Date: Wed, 5 Aug 2026 17:25:12 -0400 Subject: [PATCH] Preserve page when switching languages --- conf.py | 14 +++++++++++++- zh/conf.py | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/conf.py b/conf.py index fd1a8295..5b61b998 100644 --- a/conf.py +++ b/conf.py @@ -2,6 +2,8 @@ # Migrated off d2lbook to plain Sphinx + MyST-Parser + sphinx-book-theme. # Build: sphinx-build -b html . _build/html +from pathlib import Path + project = "Modern GPU Programming For MLSys" author = "MLC Community" copyright = "2026, MLC Community" @@ -76,7 +78,17 @@ def add_language_switch_button(app, pagename, templatename, context, doctree): if header_buttons is None: return - zh_url = context["pathto"]("zh/index.html", 1) + zh_source_root = Path(app.srcdir) / "zh" + has_translation = any( + (zh_source_root / f"{pagename}{suffix}").is_file() + for suffix in source_suffix + ) + target_page = ( + pagename + if has_translation or pagename in {"search", "genindex"} + else "index" + ) + zh_url = context["pathto"](f"zh/{target_page}.html", 1) header_buttons.append( { "type": "javascript", diff --git a/zh/conf.py b/zh/conf.py index a9014f81..9162f7ba 100644 --- a/zh/conf.py +++ b/zh/conf.py @@ -1,6 +1,8 @@ # Sphinx configuration for the Chinese edition. # Build: sphinx-build -b html zh _build/html/zh +from pathlib import Path + project = "面向 MLSys 的现代 GPU 编程" author = "MLC Community" copyright = "2026, MLC Community" @@ -58,7 +60,17 @@ def add_language_switch_button(app, pagename, templatename, context, doctree): if header_buttons is None: return - en_url = context["pathto"]("../index.html", 1) + en_source_root = Path(app.srcdir).parent + has_translation = any( + (en_source_root / f"{pagename}{suffix}").is_file() + for suffix in source_suffix + ) + target_page = ( + pagename + if has_translation or pagename in {"search", "genindex"} + else "index" + ) + en_url = context["pathto"](f"../{target_page}.html", 1) header_buttons.append( { "type": "javascript",