Update build workflow and language switch extension for improved multilingual support

This commit is contained in:
obyalian
2025-10-20 10:35:44 +08:00
parent bc490c8e18
commit 1ee8bce349
2 changed files with 29 additions and 29 deletions
+6 -6
View File
@@ -1,22 +1,21 @@
"""Sphinx extension to inject language switch links between English (en) and Chinese (zh) builds.
Works for local file:// viewing: replaces the segment /en/_build/html/ with /zh/_build/html/ and vice versa.
Removes need for hard-coded absolute paths.
Updated to work with GitHub Pages project URLs like /<repo>/en/... and /<repo>/zh/....
"""
from docutils import nodes
def setup(app):
app.connect('doctree-resolved', inject_language_switch)
return {"version": "0.3", "parallel_read_safe": True}
return {"version": "0.4", "parallel_read_safe": True}
def inject_language_switch(app, doctree, docname):
lang = app.config.language or 'en'
is_en = lang.startswith('en')
# Placeholder labels; JS will adjust depending on actual page path
other_label = '中文' if is_en else 'English'
current_label = 'English' if is_en else '中文'
# JS now detects /en/ or /zh/ segment and swaps only the first occurrence
html = (
'<div class="lang-switch" style="margin:0 0 1em 0; font-size:0.9em;">'
f'<a class="lang-other" href="#">{other_label}</a> | '
@@ -24,8 +23,9 @@ def inject_language_switch(app, doctree, docname):
'</div>'
'<script>(function(){'
"var p=window.location.pathname;"
"var isEn=p.indexOf('/en/_build/html/')!==-1;"
"var other=isEn? p.replace('/en/_build/html/','/zh/_build/html/') : p.replace('/zh/_build/html/','/en/_build/html/');"
"var isEn=p.indexOf('/en/')!==-1 && p.indexOf('/zh/')===-1;"
"if(p.indexOf('/zh/')!==-1) isEn=false;"
"var other = isEn ? p.replace(/\\/en\\//, '/zh/') : p.replace(/\\/zh\\//, '/en/');"
"var current=p;"
"var sw=document.querySelector('.lang-switch'); if(!sw) return;"
"var o=sw.querySelector('.lang-other'); var c=sw.querySelector('.lang-current');"