"""Sphinx extension to inject language switch links between English (en) and Chinese (zh) builds. Updated to work with GitHub Pages project URLs like //en/... and //zh/.... """ from docutils import nodes def setup(app): app.connect('doctree-resolved', inject_language_switch) 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') 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 = ( '' '' ) doctree.insert(0, nodes.raw('', html, format='html'))