From d3ce819a8df5cdff6fdf0a04f111eadd67675adf Mon Sep 17 00:00:00 2001 From: peijunlei Date: Sat, 5 Sep 2026 16:06:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(language-switch):=20=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/themes/dist_static/static/css/common.css | 5 +- public/themes/dist_static/static/js/common.js | 57 ++++++++++++++++++++++ .../website/1/zh/demo/components/header.html | 19 ++++++-- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/public/themes/dist_static/static/css/common.css b/public/themes/dist_static/static/css/common.css index 8e51eca..3a7ba46 100644 --- a/public/themes/dist_static/static/css/common.css +++ b/public/themes/dist_static/static/css/common.css @@ -1196,7 +1196,10 @@ body { font-weight: 400; line-height: 1; white-space: nowrap; -}.site-header__lang-active { +}.site-header__lang-opt { + color: inherit; +} +.site-header__lang-active { color: rgb(175, 181, 185); }.site-header__search { display: flex; diff --git a/public/themes/dist_static/static/js/common.js b/public/themes/dist_static/static/js/common.js index bb7bafc..60445ee 100644 --- a/public/themes/dist_static/static/js/common.js +++ b/public/themes/dist_static/static/js/common.js @@ -102,6 +102,63 @@ $(function() { $item.attr("data-expanded", String(!isExpanded)); $(this).attr("aria-expanded", String(!isExpanded)); }); + + // 语言切换:默认语言无前缀,其它语言用 /{lang}/ 前缀(与 getLang 一致) + (function initLangSwitch() { + const ALLOWED = ["zh", "tc", "en", "ja", "de", "fa", "id", "th", "ru"]; + const defaultLang = + document.querySelector(".site-header__lang")?.getAttribute("data-lang-default") || "zh"; + + const getPathLang = () => { + const seg = location.pathname.replace(/^\//, "").split("/")[0] || ""; + return ALLOWED.includes(seg) ? seg : defaultLang; + }; + + const buildLangUrl = (targetLang) => { + const parts = location.pathname.replace(/^\//, "").split("/").filter(Boolean); + if (parts.length && ALLOWED.includes(parts[0])) parts.shift(); + const rest = parts.join("/"); + let path; + if (!targetLang || targetLang === defaultLang) { + path = rest ? `/${rest}` : "/"; + } else { + path = rest ? `/${targetLang}/${rest}` : `/${targetLang}/`; + } + return path + location.search + location.hash; + }; + + const goLang = (targetLang) => { + if (!targetLang) return; + const cur = getPathLang(); + const uiCur = cur === "en" ? "en" : "zh"; + if (targetLang === uiCur || targetLang === cur) return; + location.href = buildLangUrl(targetLang); + }; + + const syncActive = () => { + const uiLang = getPathLang() === "en" ? "en" : "zh"; + $(".site-header__lang [data-lang]").removeClass("site-header__lang-active"); + $(`.site-header__lang [data-lang="${uiLang}"]`).addClass("site-header__lang-active"); + }; + + $(document).on("click", ".site-header__lang [data-lang]", function(e) { + e.preventDefault(); + e.stopPropagation(); + goLang(this.getAttribute("data-lang")); + }); + + $(document).on("click", ".site-header__lang", function(e) { + if ($(e.target).closest("[data-lang]").length) return; + goLang(getPathLang() === "en" ? "zh" : "en"); + }); + + $(document).on("click", ".site-header__extra-btn.lang, .mask-header__icon-btn.lang", function(e) { + e.preventDefault(); + goLang(getPathLang() === "en" ? "zh" : "en"); + }); + + syncActive(); + })(); // $(".section-contact-form").on("submit", function(e) { // e.preventDefault(); // const $form = $(this); diff --git a/public/themes/website/1/zh/demo/components/header.html b/public/themes/website/1/zh/demo/components/header.html index dc7c863..b49f9d7 100644 --- a/public/themes/website/1/zh/demo/components/header.html +++ b/public/themes/website/1/zh/demo/components/header.html @@ -120,7 +120,18 @@ -
@@ -499,7 +510,7 @@ 联系我们
-