Browse Source

refactor(common): simplify language switch logic to redirect to home page for selected language

master
peijunlei 2 weeks ago
parent
commit
18bbc18559
  1. 18
      public/themes/dist_static/static/js/common.js

18
public/themes/dist_static/static/js/common.js

@ -175,7 +175,7 @@ $(function() {
$subgroup.find(".nav-mega__subgroup-toggle").attr("aria-expanded", isExpanded);
});
// 语言切换:默认语言无前缀,其它语言用 /{lang}/ 前缀(与 getLang 一致
// 语言切换:始终跳到对应语言首页(避免详情页等带 id 的 URL 直接换前缀后报错
(function initLangSwitch() {
const ALLOWED = ["zh", "tc", "en", "ja", "de", "fa", "id", "th", "ru"];
const defaultLang =
@ -186,17 +186,9 @@ $(function() {
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 buildLangHomeUrl = (targetLang) => {
if (!targetLang || targetLang === defaultLang) return "/";
return `/${targetLang}`;
};
const goLang = (targetLang) => {
@ -204,7 +196,7 @@ $(function() {
const cur = getPathLang();
const uiCur = cur === "en" ? "en" : "zh";
if (targetLang === uiCur || targetLang === cur) return;
location.href = buildLangUrl(targetLang);
location.href = buildLangHomeUrl(targetLang);
};
const syncActive = () => {

Loading…
Cancel
Save