<style>
/* --- 核心显示逻辑 (增加 !important 以确保生效) --- */
/* 默认/英文模式:隐藏中文 */
.doc-cn {
display: none !important;
}
.doc-en {
display: block !important;
}
/* 中文模式:隐藏英文 */
body.lang-cn .doc-cn {
display: block !important;
}
body.lang-cn .doc-en {
display: none !important;
}
/* --- 侧边栏联动隐藏 --- */
/* 隐藏被 JS 标记为不需要显示的侧边栏链接 */
a.i18n-hidden {
display: none !important;
}
/* --- 按钮样式 (你设计的版本) --- */
#i18n-toggle {
width: 80px;
background: var(--main-background-color);
border: 1px solid transparent; /* 这里的边框颜色可以微调以匹配不同主题 */
color: var(--main-color);
border-radius: 3px; /*稍微调整圆角以更贴合 Rustdoc 风格*/
cursor: pointer;
font-family: "Fira Sans", Arial, NanumBarunGothic, sans-serif;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin-left: 10px; /* 给左边一点间距 */
}
#i18n-toggle:hover {
background-color: var(--theme-popup-border); /* 悬浮效果 */
}
#i18n-toggle::before {
display: inline-block;
width: 18px;
height: 18px;
filter: var(--settings-menu-filter);
margin: 8px; /* 稍微缩小边距 */
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M160 0c17.7 0 32 14.3 32 32l0 32 128 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-9.6 0-8.4 23.1c-16.4 45.2-41.1 86.5-72.2 122 14.2 8.8 29 16.6 44.4 23.5l50.4 22.4 62.2-140c5.1-11.6 16.6-19 29.2-19s24.1 7.4 29.2 19l128 288c7.2 16.2-.1 35.1-16.2 42.2s-35.1-.1-42.2-16.2l-20-45-157.5 0-20 45c-7.2 16.2-26.1 23.4-42.2 16.2s-23.4-26.1-16.2-42.2l39.8-89.5-50.4-22.4c-23-10.2-45-22.4-65.8-36.4-21.3 17.2-44.6 32.2-69.5 44.7L78.3 380.6c-15.8 7.9-35 1.5-42.9-14.3s-1.5-35 14.3-42.9l34.5-17.3c16.3-8.2 31.8-17.7 46.4-28.3-13.8-12.7-26.8-26.4-38.9-40.9L81.6 224.7c-11.3-13.6-9.5-33.8 4.1-45.1s33.8-9.5 45.1 4.1l10.2 12.2c11.5 13.9 24.1 26.8 37.4 38.7 27.5-30.4 49.2-66.1 63.5-105.4l.5-1.2-210.3 0C14.3 128 0 113.7 0 96S14.3 64 32 64l96 0 0-32c0-17.7 14.3-32 32-32zM416 270.8L365.7 384 466.3 384 416 270.8z"/></svg>');
}
</style>
<script>
(function () {
const STORAGE_KEY = "rustdoc-lang-preference";
// 1. 获取用户偏好
function getPreferredLang() {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved === "cn" || saved === "en") return saved;
const userLang = navigator.language || navigator.userLanguage;
if (userLang && userLang.toLowerCase().startsWith("zh"))
return "cn";
return "en";
}
// --- 新增:侧边栏处理逻辑 ---
function updateSidebar(lang) {
// 查找侧边栏的所有链接
const sidebarLinks = document.querySelectorAll(
".sidebar a, .sidebar-elems a",
);
sidebarLinks.forEach((link) => {
const href = link.getAttribute("href");
if (!href || !href.startsWith("#")) return;
const targetId = href.substring(1);
const targetElem = document.getElementById(targetId);
if (targetElem) {
// 检查标题是否位于特定的语言块中
const isEnContext = targetElem.closest(".doc-en");
const isCnContext = targetElem.closest(".doc-cn");
let shouldHide = false;
if (lang === "cn" && isEnContext) shouldHide = true;
if (lang === "en" && isCnContext) shouldHide = true;
if (shouldHide) {
link.classList.add("i18n-hidden");
} else {
link.classList.remove("i18n-hidden");
}
}
});
}
// 2. 设置语言视图
function setLang(lang) {
// 设置 body 类
if (lang === "cn") {
document.body.classList.add("lang-cn");
document.body.classList.remove("lang-en");
} else {
document.body.classList.remove("lang-cn");
document.body.classList.add("lang-en");
}
// 更新按钮文字 (增加检查,防止按钮未生成时报错)
const btnContent = document.getElementById("i18n-toggle-content");
if (btnContent) {
btnContent.innerText =
lang === "cn" ? "Switch to English" : "切换到 中文";
}
// 保存偏好
localStorage.setItem(STORAGE_KEY, lang);
// 更新侧边栏
updateSidebar(lang);
}
// 3. 初始化逻辑
window.addEventListener("DOMContentLoaded", () => {
const currentLang = getPreferredLang();
// 查找注入点:优先找 rustdoc-toolbar
const toolbar =
document.querySelector(".right-buttons") ||
document.querySelector("rustdoc-toolbar");
if (toolbar) {
const btn = document.createElement("button");
btn.id = "i18n-toggle";
btn.title = "Toggle Language / 切换语言"; // 增加鼠标悬浮提示
const content = document.createElement("span");
content.id = "i18n-toggle-content";
content.classList.add("label");
btn.appendChild(content);
btn.onclick = () => {
const isCn = document.body.classList.contains("lang-cn");
setLang(isCn ? "en" : "cn");
};
// 插入到工具栏的最前面或最后面
toolbar.appendChild(btn);
}
// 应用初始语言
setLang(currentLang);
});
})();
</script>