Add language switch extension for bilingual documentation support

This commit is contained in:
obyalian
2025-10-19 17:47:12 +08:00
parent b7bc29ae1c
commit 2369304b92
5 changed files with 46 additions and 43 deletions
+36
View File
@@ -0,0 +1,36 @@
"""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.
"""
from docutils import nodes
def setup(app):
app.connect('doctree-resolved', inject_language_switch)
return {"version": "0.3", "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 '中文'
html = (
'<div class="lang-switch" style="margin:0 0 1em 0; font-size:0.9em;">'
f'<a class="lang-other" href="#">{other_label}</a> | '
f'<a class="lang-current" href="#">{current_label}</a>'
'</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 current=p;"
"var sw=document.querySelector('.lang-switch'); if(!sw) return;"
"var o=sw.querySelector('.lang-other'); var c=sw.querySelector('.lang-current');"
"if(isEn){ o.textContent='中文'; c.textContent='English'; } else { o.textContent='English'; c.textContent='中文'; }"
"o.setAttribute('href', other); c.setAttribute('href', current);"
'})();</script>'
)
doctree.insert(0, nodes.raw('', html, format='html'))
+5 -2
View File
@@ -18,6 +18,10 @@ import os
import shutil
import sphinx
# Add extension path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '_ext'))
import recommonmark
from recommonmark.transform import AutoStructify
@@ -50,9 +54,8 @@ master_doc = 'index'
# ones.
extensions = ['recommonmark',
'sphinx_markdown_tables',
'sphinx.ext.autosectionlabel',
'language_switch',
# 'myst_parser',
]
-39
View File
@@ -1,39 +0,0 @@
// Language switch for English version
document.addEventListener('DOMContentLoaded', function() {
// Create language switch link
var langSwitch = document.createElement('div');
langSwitch.className = 'language-switch';
langSwitch.innerHTML = '<a href="/OrbbecSDK_ROS2/zh/" title="切换到中文">中文</a>';
// Add styles
var style = document.createElement('style');
style.textContent = `
.language-switch {
position: fixed;
top: 10px;
right: 60px;
z-index: 1000;
}
.language-switch a {
color: #fff;
background: #2980b9;
padding: 5px 15px;
border-radius: 3px;
text-decoration: none;
font-size: 14px;
transition: background 0.3s;
}
.language-switch a:hover {
background: #3498db;
}
@media screen and (max-width: 768px) {
.language-switch {
top: 5px;
right: 10px;
}
}
`;
document.head.appendChild(style);
document.body.appendChild(langSwitch);
});
+5 -2
View File
@@ -18,6 +18,10 @@ import os
import shutil
import sphinx
# Add extension path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '_ext'))
import recommonmark
from recommonmark.transform import AutoStructify
@@ -50,9 +54,8 @@ master_doc = 'index'
# ones.
extensions = ['recommonmark',
'sphinx_markdown_tables',
'sphinx.ext.autosectionlabel',
'language_switch',
# 'myst_parser',
]