mirror of
https://github.com/ultralytics/ultralytics
synced 2026-04-21 14:07:18 +00:00
Fix language switch HTML solution with JS (#20504)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
91d60f872c
commit
149dd26afe
4 changed files with 71 additions and 44 deletions
|
|
@ -224,35 +224,6 @@ def update_docs_soup(content: str, html_file: Path = None, max_title_length: int
|
|||
a.replace_with(soup.new_tag("span")) # Replace with an empty span
|
||||
modified = True
|
||||
|
||||
# Fix language switcher links
|
||||
if html_file:
|
||||
# Get page path without index.html
|
||||
current_path = file_path = "/" + str(html_file.relative_to(SITE)).replace("\\", "/").replace("index.html", "")
|
||||
|
||||
# Get language links and extract language codes
|
||||
lang_links = soup.select(".md-select__link")
|
||||
lang_codes = [m.group(1) for link in lang_links if (m := re.match(r"^/([a-z]{2})/?$", link.get("href", "")))]
|
||||
|
||||
# Find current language and path
|
||||
for lang in lang_codes:
|
||||
if file_path.startswith(f"/{lang}/"):
|
||||
current_path = file_path[len(f"/{lang}") :]
|
||||
break
|
||||
|
||||
# Update all language links
|
||||
for link in lang_links:
|
||||
href = link.get("href", "")
|
||||
match = re.match(r"^/([a-z]{2})/?$", href)
|
||||
|
||||
if match:
|
||||
# Language root link (e.g., /zh/)
|
||||
link["href"] = f"/{match.group(1)}{current_path}"
|
||||
modified = True
|
||||
elif href == "/" or href == "":
|
||||
# Default language link
|
||||
link["href"] = current_path
|
||||
modified = True
|
||||
|
||||
return str(soup) if modified else content
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -67,6 +67,9 @@
|
|||
40165666+berry-ding@users.noreply.github.com:
|
||||
avatar: https://avatars.githubusercontent.com/u/40165666?v=4
|
||||
username: berry-ding
|
||||
40582518+erfan-zekri@users.noreply.github.com:
|
||||
avatar: https://avatars.githubusercontent.com/u/40582518?v=4
|
||||
username: erfan-zekri
|
||||
44016758+M-Amrollahi@users.noreply.github.com:
|
||||
avatar: https://avatars.githubusercontent.com/u/44016758?v=4
|
||||
username: M-Amrollahi
|
||||
|
|
|
|||
|
|
@ -71,8 +71,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
const config = {
|
||||
baseSettings: {
|
||||
apiKey: "13dfec2e75982bc9bae3199a08e13b86b5fbacd64e9b2f89",
|
||||
integrationId: "cm1shscmm00y26sj83lgxzvkw",
|
||||
organizationId: "org_e3869az6hQZ0mXdF",
|
||||
primaryBrandColor: "#E1FF25",
|
||||
organizationDisplayName: "Ultralytics",
|
||||
colorMode: {
|
||||
|
|
@ -164,3 +162,58 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
widgetContainer && Inkeep.SearchBar("#inkeepSearchBar", config);
|
||||
});
|
||||
});
|
||||
|
||||
// Fix language switcher links
|
||||
(function () {
|
||||
function fixLanguageLinks() {
|
||||
const path = location.pathname;
|
||||
const links = document.querySelectorAll(".md-select__link");
|
||||
if (!links.length) return;
|
||||
|
||||
const langs = [];
|
||||
let defaultLink = null;
|
||||
|
||||
// Extract language codes
|
||||
links.forEach((link) => {
|
||||
const href = link.getAttribute("href");
|
||||
if (!href) return;
|
||||
|
||||
const url = new URL(href, location.origin);
|
||||
const match = url.pathname.match(/^\/([a-z]{2})\/?$/);
|
||||
|
||||
if (match) langs.push({ code: match[1], link });
|
||||
else if (url.pathname === "/" || url.pathname === "") defaultLink = link;
|
||||
});
|
||||
|
||||
// Find current language and base path
|
||||
let basePath = path;
|
||||
for (const lang of langs) {
|
||||
if (path.startsWith("/" + lang.code + "/")) {
|
||||
basePath = path.substring(lang.code.length + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Update links
|
||||
langs.forEach(
|
||||
(lang) => (lang.link.href = location.origin + "/" + lang.code + basePath),
|
||||
);
|
||||
if (defaultLink) defaultLink.href = location.origin + basePath;
|
||||
}
|
||||
|
||||
// Run immediately
|
||||
fixLanguageLinks();
|
||||
|
||||
// Handle SPA navigation
|
||||
if (typeof document$ !== "undefined") {
|
||||
document$.subscribe(() => setTimeout(fixLanguageLinks, 50));
|
||||
} else {
|
||||
let lastPath = location.pathname;
|
||||
setInterval(() => {
|
||||
if (location.pathname !== lastPath) {
|
||||
lastPath = location.pathname;
|
||||
setTimeout(fixLanguageLinks, 50);
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
26
mkdocs.yml
26
mkdocs.yml
|
|
@ -98,43 +98,43 @@ extra: # version:
|
|||
- icon: fontawesome/brands/weixin
|
||||
link: https://weixin.qq.com/r/mp/LxckPDfEgWr_rXNf90I9
|
||||
alternate:
|
||||
- name: English
|
||||
- name: 🇬🇧 English
|
||||
link: /
|
||||
lang: en
|
||||
- name: 简体中文
|
||||
- name: 🇨🇳 简体中文
|
||||
link: /zh/
|
||||
lang: zh
|
||||
- name: 한국어
|
||||
- name: 🇰🇷 한국어
|
||||
link: /ko/
|
||||
lang: ko
|
||||
- name: 日本語
|
||||
- name: 🇯🇵 日本語
|
||||
link: /ja/
|
||||
lang: ja
|
||||
- name: Русский
|
||||
- name: 🇷🇺 Русский
|
||||
link: /ru/
|
||||
lang: ru
|
||||
- name: Deutsch
|
||||
- name: 🇩🇪 Deutsch
|
||||
link: /de/
|
||||
lang: de
|
||||
- name: Français
|
||||
- name: 🇫🇷 Français
|
||||
link: /fr/
|
||||
lang: fr
|
||||
- name: Español
|
||||
- name: 🇪🇸 Español
|
||||
link: /es/
|
||||
lang: es
|
||||
- name: Português
|
||||
- name: 🇵🇹 Português
|
||||
link: /pt/
|
||||
lang: pt
|
||||
- name: Italiano
|
||||
- name: 🇮🇹 Italiano
|
||||
link: /it/
|
||||
lang: it
|
||||
- name: Türkçe
|
||||
- name: 🇹🇷 Türkçe
|
||||
link: /tr/
|
||||
lang: tr
|
||||
- name: Tiếng Việt
|
||||
- name: 🇻🇳 Tiếng Việt
|
||||
link: /vi/
|
||||
lang: vi
|
||||
- name: العربية
|
||||
- name: 🇸🇦 العربية
|
||||
link: /ar/
|
||||
lang: ar
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue