Skip to content

Commit 9d1ec7e

Browse files
committed
Add translation support by mdbook-i18n-helpers to bootstrap
1 parent 865eaf9 commit 9d1ec7e

File tree

4 files changed

+186
-16
lines changed

4 files changed

+186
-16
lines changed

Cargo.lock

+143-5
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
372372
checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05"
373373
dependencies = [
374374
"memchr",
375-
"regex-automata 0.3.9",
375+
"regex-automata 0.3.7",
376376
"serde",
377377
]
378378

@@ -2206,6 +2206,21 @@ dependencies = [
22062206
"vcpkg",
22072207
]
22082208

2209+
[[package]]
2210+
name = "line-wrap"
2211+
version = "0.2.0"
2212+
source = "registry+https://github.com/rust-lang/crates.io-index"
2213+
checksum = "dd1bc4d24ad230d21fb898d1116b1801d7adfc449d42026475862ab48b11e70e"
2214+
2215+
[[package]]
2216+
name = "linereader"
2217+
version = "0.4.0"
2218+
source = "registry+https://github.com/rust-lang/crates.io-index"
2219+
checksum = "d921fea6860357575519aca014c6e22470585accdd543b370c404a8a72d0dd1d"
2220+
dependencies = [
2221+
"memchr",
2222+
]
2223+
22092224
[[package]]
22102225
name = "linkchecker"
22112226
version = "0.1.0"
@@ -2214,6 +2229,12 @@ dependencies = [
22142229
"regex",
22152230
]
22162231

2232+
[[package]]
2233+
name = "linked-hash-map"
2234+
version = "0.5.6"
2235+
source = "registry+https://github.com/rust-lang/crates.io-index"
2236+
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
2237+
22172238
[[package]]
22182239
name = "lint-docs"
22192240
version = "0.1.0"
@@ -2364,6 +2385,25 @@ dependencies = [
23642385
"topological-sort",
23652386
]
23662387

2388+
[[package]]
2389+
name = "mdbook-i18n-helpers"
2390+
version = "0.3.3"
2391+
source = "registry+https://github.com/rust-lang/crates.io-index"
2392+
checksum = "f1f71f5961d6f3376e1ff3e5989c2e3ecccc3e8a00f3a3acde446847f84852e4"
2393+
dependencies = [
2394+
"anyhow",
2395+
"chrono",
2396+
"mdbook",
2397+
"polib",
2398+
"pulldown-cmark 0.10.3",
2399+
"pulldown-cmark-to-cmark",
2400+
"regex",
2401+
"semver",
2402+
"serde_json",
2403+
"syntect",
2404+
"textwrap",
2405+
]
2406+
23672407
[[package]]
23682408
name = "mdbook-trpl-listing"
23692409
version = "0.1.0"
@@ -2650,6 +2690,28 @@ version = "1.19.0"
26502690
source = "registry+https://github.com/rust-lang/crates.io-index"
26512691
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
26522692

2693+
[[package]]
2694+
name = "onig"
2695+
version = "6.4.0"
2696+
source = "registry+https://github.com/rust-lang/crates.io-index"
2697+
checksum = "8c4b31c8722ad9171c6d77d3557db078cab2bd50afcc9d09c8b315c59df8ca4f"
2698+
dependencies = [
2699+
"bitflags 1.3.2",
2700+
"libc",
2701+
"once_cell",
2702+
"onig_sys",
2703+
]
2704+
2705+
[[package]]
2706+
name = "onig_sys"
2707+
version = "69.8.1"
2708+
source = "registry+https://github.com/rust-lang/crates.io-index"
2709+
checksum = "7b829e3d7e9cc74c7e315ee8edb185bf4190da5acde74afd7fc59c35b1f086e7"
2710+
dependencies = [
2711+
"cc",
2712+
"pkg-config",
2713+
]
2714+
26532715
[[package]]
26542716
name = "opener"
26552717
version = "0.6.1"
@@ -2968,6 +3030,29 @@ version = "0.3.30"
29683030
source = "registry+https://github.com/rust-lang/crates.io-index"
29693031
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
29703032

3033+
[[package]]
3034+
name = "plist"
3035+
version = "1.6.1"
3036+
source = "registry+https://github.com/rust-lang/crates.io-index"
3037+
checksum = "d9d34169e64b3c7a80c8621a48adaf44e0cf62c78a9b25dd9dd35f1881a17cf9"
3038+
dependencies = [
3039+
"base64",
3040+
"indexmap",
3041+
"line-wrap",
3042+
"quick-xml",
3043+
"serde",
3044+
"time",
3045+
]
3046+
3047+
[[package]]
3048+
name = "polib"
3049+
version = "0.2.0"
3050+
source = "registry+https://github.com/rust-lang/crates.io-index"
3051+
checksum = "6b393b155cf9be86249cba1b56cc81be0e6212c66d94ac0d76d37a1761f3bb1b"
3052+
dependencies = [
3053+
"linereader",
3054+
]
3055+
29713056
[[package]]
29723057
name = "polonius-engine"
29733058
version = "0.13.0"
@@ -3126,6 +3211,15 @@ version = "0.4.1"
31263211
source = "registry+https://github.com/rust-lang/crates.io-index"
31273212
checksum = "e9e1dcb320d6839f6edb64f7a4a59d39b30480d4d1765b56873f7c858538a5fe"
31283213

3214+
[[package]]
3215+
name = "quick-xml"
3216+
version = "0.31.0"
3217+
source = "registry+https://github.com/rust-lang/crates.io-index"
3218+
checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33"
3219+
dependencies = [
3220+
"memchr",
3221+
]
3222+
31293223
[[package]]
31303224
name = "quine-mc_cluskey"
31313225
version = "0.2.4"
@@ -3261,12 +3355,13 @@ dependencies = [
32613355

32623356
[[package]]
32633357
name = "regex"
3264-
version = "1.8.4"
3358+
version = "1.9.4"
32653359
source = "registry+https://github.com/rust-lang/crates.io-index"
3266-
checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
3360+
checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29"
32673361
dependencies = [
32683362
"aho-corasick",
32693363
"memchr",
3364+
"regex-automata 0.3.7",
32703365
"regex-syntax 0.7.5",
32713366
]
32723367

@@ -3290,9 +3385,14 @@ dependencies = [
32903385

32913386
[[package]]
32923387
name = "regex-automata"
3293-
version = "0.3.9"
3388+
version = "0.3.7"
32943389
source = "registry+https://github.com/rust-lang/crates.io-index"
3295-
checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
3390+
checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629"
3391+
dependencies = [
3392+
"aho-corasick",
3393+
"memchr",
3394+
"regex-syntax 0.7.5",
3395+
]
32963396

32973397
[[package]]
32983398
name = "regex-lite"
@@ -3367,6 +3467,7 @@ dependencies = [
33673467
"clap",
33683468
"env_logger",
33693469
"mdbook",
3470+
"mdbook-i18n-helpers",
33703471
"mdbook-trpl-listing",
33713472
"mdbook-trpl-note",
33723473
]
@@ -5372,6 +5473,28 @@ dependencies = [
53725473
"syn 2.0.64",
53735474
]
53745475

5476+
[[package]]
5477+
name = "syntect"
5478+
version = "5.2.0"
5479+
source = "registry+https://github.com/rust-lang/crates.io-index"
5480+
checksum = "874dcfa363995604333cf947ae9f751ca3af4522c60886774c4963943b4746b1"
5481+
dependencies = [
5482+
"bincode",
5483+
"bitflags 1.3.2",
5484+
"flate2",
5485+
"fnv",
5486+
"once_cell",
5487+
"onig",
5488+
"plist",
5489+
"regex-syntax 0.8.3",
5490+
"serde",
5491+
"serde_derive",
5492+
"serde_json",
5493+
"thiserror",
5494+
"walkdir",
5495+
"yaml-rust",
5496+
]
5497+
53755498
[[package]]
53765499
name = "sysinfo"
53775500
version = "0.30.12"
@@ -5497,6 +5620,12 @@ dependencies = [
54975620
"std",
54985621
]
54995622

5623+
[[package]]
5624+
name = "textwrap"
5625+
version = "0.16.1"
5626+
source = "registry+https://github.com/rust-lang/crates.io-index"
5627+
checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
5628+
55005629
[[package]]
55015630
name = "thin-vec"
55025631
version = "0.2.13"
@@ -6476,6 +6605,15 @@ dependencies = [
64766605
"lzma-sys",
64776606
]
64786607

6608+
[[package]]
6609+
name = "yaml-rust"
6610+
version = "0.4.5"
6611+
source = "registry+https://github.com/rust-lang/crates.io-index"
6612+
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
6613+
dependencies = [
6614+
"linked-hash-map",
6615+
]
6616+
64796617
[[package]]
64806618
name = "yansi-term"
64816619
version = "0.1.2"

src/bootstrap/src/core/build_steps/doc.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ macro_rules! submodule_helper {
2929
}
3030

3131
macro_rules! book {
32-
($($name:ident, $path:expr, $book_name:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
32+
($($name:ident, $path:expr, $book_name:expr, $lang:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
3333
$(
3434
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
3535
pub struct $name {
@@ -61,6 +61,7 @@ macro_rules! book {
6161
name: $book_name.to_owned(),
6262
src: builder.src.join($path),
6363
parent: Some(self),
64+
languages: $lang.into(),
6465
})
6566
}
6667
}
@@ -74,15 +75,15 @@ macro_rules! book {
7475
// FIXME: Make checking for a submodule automatic somehow (maybe by having a list of all submodules
7576
// and checking against it?).
7677
book!(
77-
CargoBook, "src/tools/cargo/src/doc", "cargo", submodule = "src/tools/cargo";
78-
ClippyBook, "src/tools/clippy/book", "clippy";
79-
EditionGuide, "src/doc/edition-guide", "edition-guide", submodule;
80-
EmbeddedBook, "src/doc/embedded-book", "embedded-book", submodule;
81-
Nomicon, "src/doc/nomicon", "nomicon", submodule;
82-
Reference, "src/doc/reference", "reference", submodule;
83-
RustByExample, "src/doc/rust-by-example", "rust-by-example", submodule;
84-
RustdocBook, "src/doc/rustdoc", "rustdoc";
85-
StyleGuide, "src/doc/style-guide", "style-guide";
78+
CargoBook, "src/tools/cargo/src/doc", "cargo", &[], submodule = "src/tools/cargo";
79+
ClippyBook, "src/tools/clippy/book", "clippy", &[];
80+
EditionGuide, "src/doc/edition-guide", "edition-guide", &[], submodule;
81+
EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[], submodule;
82+
Nomicon, "src/doc/nomicon", "nomicon", &[], submodule;
83+
Reference, "src/doc/reference", "reference", &[], submodule;
84+
RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja"], submodule;
85+
RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
86+
StyleGuide, "src/doc/style-guide", "style-guide", &[];
8687
);
8788

8889
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@@ -110,6 +111,7 @@ impl Step for UnstableBook {
110111
name: "unstable-book".to_owned(),
111112
src: builder.md_doc_out(self.target).join("unstable-book"),
112113
parent: Some(self),
114+
languages: vec![],
113115
})
114116
}
115117
}
@@ -120,6 +122,7 @@ struct RustbookSrc<P: Step> {
120122
name: String,
121123
src: PathBuf,
122124
parent: Option<P>,
125+
languages: Vec<&'static str>,
123126
}
124127

125128
impl<P: Step> Step for RustbookSrc<P> {
@@ -151,7 +154,19 @@ impl<P: Step> Step for RustbookSrc<P> {
151154
builder.info(&format!("Rustbook ({target}) - {name}"));
152155
let _ = fs::remove_dir_all(&out);
153156

154-
builder.run(rustbook_cmd.arg("build").arg(src).arg("-d").arg(out));
157+
builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(&out));
158+
159+
for lang in &self.languages {
160+
let out = out.join(lang);
161+
162+
builder.info(&format!("Rustbook ({target}) - {name} - {lang}"));
163+
let _ = fs::remove_dir_all(&out);
164+
165+
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
166+
builder.run(
167+
rustbook_cmd.arg("build").arg(&src).arg("-d").arg(&out).arg("-l").arg(lang),
168+
);
169+
}
155170
}
156171

157172
if self.parent.is_some() {
@@ -214,6 +229,7 @@ impl Step for TheBook {
214229
name: "book".to_owned(),
215230
src: absolute_path.clone(),
216231
parent: Some(self),
232+
languages: vec![],
217233
});
218234

219235
// building older edition redirects
@@ -225,6 +241,7 @@ impl Step for TheBook {
225241
// There should only be one book that is marked as the parent for each target, so
226242
// treat the other editions as not having a parent.
227243
parent: Option::<Self>::None,
244+
languages: vec![],
228245
});
229246
}
230247

@@ -1208,6 +1225,7 @@ impl Step for RustcBook {
12081225
name: "rustc".to_owned(),
12091226
src: out_base,
12101227
parent: Some(self),
1228+
languages: vec![],
12111229
});
12121230
}
12131231
}

src/tools/rustbook/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ clap = "4.0.32"
99
env_logger = "0.11"
1010
mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" }
1111
mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" }
12+
mdbook-i18n-helpers = "0.3.3"
1213

1314
[dependencies.mdbook]
1415
version = "0.4.37"

src/tools/rustbook/src/main.rs

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clap::{arg, ArgMatches, Command};
77

88
use mdbook::errors::Result as Result3;
99
use mdbook::MDBook;
10+
use mdbook_i18n_helpers::preprocessors::Gettext;
1011

1112
use mdbook_trpl_listing::TrplListing;
1213
use mdbook_trpl_note::TrplNote;
@@ -19,6 +20,11 @@ fn main() {
1920
.required(false)
2021
.value_parser(clap::value_parser!(PathBuf));
2122

23+
let l_arg = arg!(-l --"lang" <LANGUAGE>
24+
"The output language")
25+
.required(false)
26+
.value_parser(clap::value_parser!(String));
27+
2228
let dir_arg = arg!([dir] "Root directory for the book\n\
2329
(Defaults to the current directory when omitted)")
2430
.value_parser(clap::value_parser!(PathBuf));
@@ -33,6 +39,7 @@ fn main() {
3339
Command::new("build")
3440
.about("Build the book from the markdown files")
3541
.arg(d_arg)
42+
.arg(l_arg)
3643
.arg(&dir_arg),
3744
)
3845
.subcommand(
@@ -63,6 +70,12 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
6370
let book_dir = get_book_dir(args);
6471
let mut book = load_book(&book_dir)?;
6572

73+
if let Some(lang) = args.get_one::<String>("lang") {
74+
let gettext = Gettext;
75+
book.with_preprocessor(gettext);
76+
book.config.set("book.language", lang).unwrap();
77+
}
78+
6679
// Set this to allow us to catch bugs in advance.
6780
book.config.build.create_missing = false;
6881

0 commit comments

Comments
 (0)