Skip to content

Commit ff7c94a

Browse files
committed
1 parent 9bfb8e3 commit ff7c94a

File tree

6 files changed

+76
-76
lines changed

6 files changed

+76
-76
lines changed

.github/workflows/rbe.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ env:
55
# Update the language picker in index.hbs to link new languages.
66
LANGUAGES: ja zh es
77

8+
env:
9+
MDBOOK_VERSION: 0.5.1
10+
811
jobs:
912
test:
1013
name: Run tests
@@ -27,12 +30,12 @@ jobs:
2730
- name: Install mdbook
2831
run: |
2932
mkdir bin
30-
curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.51/mdbook-v0.4.51-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
33+
curl -sSL https://github.com/rust-lang/mdBook/releases/download/v${MDBOOK_VERSION}/mdbook-v${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
3134
echo "$(pwd)/bin" >> ${GITHUB_PATH}
3235
3336
- name: Install mdbook-i18n-helpers
3437
run: |
35-
cargo install mdbook-i18n-helpers --locked --version 0.3.4
38+
cargo install mdbook-i18n-helpers --locked --version 0.4.0
3639
3740
- name: Report versions
3841
run: |

CONTRIBUTING.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ $ mdbook build
5656
**The following warnings can be ignored safely.**
5757

5858
```text
59-
[WARN] (mdbook::preprocess::cmd): The command wasn't found, is the "gettext" preprocessor installed?
60-
[WARN] (mdbook::preprocess::cmd): Command: mdbook-gettext
59+
WARN The command `mdbook-gettext` for preprocessor `gettext` was not found, but is marked as optional.
6160
```
6261

6362
[install Rust]: http://rust-lang.org/install.html

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ read all content offline, however!
2727
**The following warnings can be ignored safely.**
2828

2929
```text
30-
[WARN] (mdbook::preprocess::cmd): The command wasn't found, is the "gettext" preprocessor installed?
31-
[WARN] (mdbook::preprocess::cmd): Command: mdbook-gettext
30+
WARN The command `mdbook-gettext` for preprocessor `gettext` was not found, but is marked as optional.
3231
```
3332

3433
### Using translated version

book.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ hash-files = true
1717
additional-css = [
1818
"theme/css/language-picker.css",
1919
]
20-
additional-js = ["theme/js/language-picker.js"]
2120

2221
[rust]
2322
edition = "2021"

theme/head.hbs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,71 @@
11
<script>
2-
const mdbookPath = "{{ path }}";
3-
const mdbookPathToRoot = "{{ path_to_root }}";
2+
document.addEventListener('DOMContentLoaded', function() {
3+
const mdbookPath = "{{ path }}";
4+
const mdbookPathToRoot = "{{ path_to_root }}";
5+
const rightButtonsElement = document.querySelector('.right-buttons');
6+
rightButtonsElement.insertAdjacentHTML("afterbegin", `
7+
<button id="language-toggle" class="icon-button" type="button"
8+
title="Change language" aria-label="Change language"
9+
aria-haspopup="true" aria-expanded="false"
10+
aria-controls="language-list">
11+
{{fa "solid" "globe"}}
12+
</button>
13+
<ul id="language-list" class="theme-popup" aria-label="Languages" role="menu">
14+
<li role="none"><button role="menuitem" class="theme">
15+
<a id="en">English</a>
16+
</button></li>
17+
<li role="none"><button role="menuitem" class="theme">
18+
<a id="ja">日本語</a>
19+
</button></li>
20+
<li role="none"><button role="menuitem" class="theme">
21+
<a id="zh">中文</a>
22+
</button></li>
23+
<li role="none"><button role="menuitem" class="theme">
24+
<a id="es">Español</a>
25+
</button></li>
26+
</ul>
27+
`);
28+
29+
const language = document.documentElement.getAttribute("lang");
30+
let langToggle = document.getElementById("language-toggle");
31+
let langList = document.getElementById("language-list");
32+
langToggle.addEventListener("click", (event) => {
33+
langList.style.display =
34+
langList.style.display == "block" ? "none" : "block";
35+
});
36+
let selectedLang = document.getElementById(language);
37+
if (selectedLang) {
38+
selectedLang.parentNode.classList.add("theme-selected");
39+
}
40+
41+
// The path to the root, taking the current language into account.
42+
let full_path_to_root =
43+
language == "en" ? `${mdbookPathToRoot}` : `${mdbookPathToRoot}../`;
44+
// The page path (mdbook only gives us access to the path to the Markdown file).
45+
let path = mdbookPath.replace(/\.md$/, ".html");
46+
const langAnchors = Array.from(langList.querySelectorAll("a"));
47+
for (let lang of langAnchors) {
48+
if (lang.id == "en") {
49+
lang.href = `${full_path_to_root}${path}`;
50+
} else {
51+
lang.href = `${full_path_to_root}${lang.id}/${path}`;
52+
}
53+
}
54+
55+
// Hide languages whose target page is not available (e.g., not deployed).
56+
// This prevents users from hitting 404s on sites that only ship some locales.
57+
for (let lang of langAnchors) {
58+
const url = lang.href;
59+
// Attempt a lightweight HEAD request; fall back to hiding on failure.
60+
fetch(url, { method: "HEAD" }).then((resp) => {
61+
if (!resp.ok) {
62+
const li = lang.parentNode && lang.parentNode.parentNode;
63+
if (li) li.style.display = "none";
64+
}
65+
}).catch(() => {
66+
const li = lang.parentNode && lang.parentNode.parentNode;
67+
if (li) li.style.display = "none";
68+
});
69+
}
70+
});
471
</script>

theme/js/language-picker.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)