Skip to content

Commit 9ff7027

Browse files
authored
Fix- Spanish translation doesn't work #1965
I added a runtime check in the language picker so unavailable locales are hidden, preventing 404s like the Spanish URL on doc.rust-lang.org. This keeps working languages (ja/zh) visible and avoids broken links when a locale isn’t deployed. To verify locally - English-only build: run mdbook serve and confirm the menu hides ja/zh/es. - Japanese build: MDBOOK_BOOK__LANGUAGE=ja mdbook serve and confirm only ja (and any other deployed locales) remain. - Spanish build: MDBOOK_BOOK__LANGUAGE=es mdbook serve and confirm es remains and others hide.
1 parent 5dc070e commit 9ff7027

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

theme/js/language-picker.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,28 @@
4040
language == "en" ? `${mdbookPathToRoot}` : `${mdbookPathToRoot}../`;
4141
// The page path (mdbook only gives us access to the path to the Markdown file).
4242
let path = mdbookPath.replace(/\.md$/, ".html");
43-
for (let lang of langList.querySelectorAll("a")) {
43+
const langAnchors = Array.from(langList.querySelectorAll("a"));
44+
for (let lang of langAnchors) {
4445
if (lang.id == "en") {
4546
lang.href = `${full_path_to_root}${path}`;
4647
} else {
4748
lang.href = `${full_path_to_root}${lang.id}/${path}`;
4849
}
4950
}
51+
52+
// Hide languages whose target page is not available (e.g., not deployed).
53+
// This prevents users from hitting 404s on sites that only ship some locales.
54+
for (let lang of langAnchors) {
55+
const url = lang.href;
56+
// Attempt a lightweight HEAD request; fall back to hiding on failure.
57+
fetch(url, { method: "HEAD" }).then((resp) => {
58+
if (!resp.ok) {
59+
const li = lang.parentNode && lang.parentNode.parentNode;
60+
if (li) li.style.display = "none";
61+
}
62+
}).catch(() => {
63+
const li = lang.parentNode && lang.parentNode.parentNode;
64+
if (li) li.style.display = "none";
65+
});
66+
}
5067
})();

0 commit comments

Comments
 (0)