Skip to content

Commit 1458b74

Browse files
committed
Add submodule for document translation
1 parent 83965ef commit 1458b74

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@
5555
path = src/gcc
5656
url = https://github.com/rust-lang/gcc.git
5757
shallow = true
58+
[submodule "src/doc/translations"]
59+
path = src/doc/translations
60+
url = https://github.com/dalance/translations

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

+6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ impl<P: Step> Step for RustbookSrc<P> {
177177
.arg(&out)
178178
.arg("--rust-root")
179179
.arg(&builder.src)
180+
.arg("-n")
181+
.arg(&name)
180182
.run(builder);
181183

182184
for lang in &self.languages {
@@ -191,6 +193,10 @@ impl<P: Step> Step for RustbookSrc<P> {
191193
.arg(&src)
192194
.arg("-d")
193195
.arg(&out)
196+
.arg("--rust-root")
197+
.arg(&builder.src)
198+
.arg("-n")
199+
.arg(&name)
194200
.arg("-l")
195201
.arg(lang)
196202
.run(builder);

src/doc/translations

Submodule translations added at 3afda44

src/tools/rustbook/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ dependencies = [
11461146
"mdbook-spec",
11471147
"mdbook-trpl-listing",
11481148
"mdbook-trpl-note",
1149+
"toml 0.5.11",
11491150
]
11501151

11511152
[[package]]

src/tools/rustbook/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" }
1313
mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" }
1414
mdbook-i18n-helpers = "0.3.3"
1515
mdbook-spec = { path = "../../doc/reference/mdbook-spec" }
16+
toml = "0.5.11"
1617

1718
[dependencies.mdbook]
1819
version = "0.4.37"

src/tools/rustbook/src/main.rs

+52
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ fn main() {
2222
.required(false)
2323
.value_parser(clap::value_parser!(String));
2424

25+
let n_arg = arg!(-n --"name" <NAME>
26+
"The book name")
27+
.required(false)
28+
.value_parser(clap::value_parser!(String));
29+
2530
let root_arg = arg!(--"rust-root" <ROOT_DIR>
2631
"Path to the root of the rust source tree")
2732
.required(false)
@@ -56,6 +61,7 @@ fn main() {
5661
.about("Build the book from the markdown files")
5762
.arg(d_arg)
5863
.arg(l_arg)
64+
.arg(n_arg)
5965
.arg(root_arg)
6066
.arg(&dir_arg),
6167
)
@@ -121,8 +127,54 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
121127
book.with_preprocessor(Spec::new(rust_root)?);
122128
}
123129

130+
let mut cleanup = Vec::new();
131+
if let Some(rust_root) = args.get_one::<PathBuf>("rust-root") {
132+
if let Some(name) = args.get_one::<String>("name") {
133+
let translation_path = rust_root.join(format!("src/doc/translations/{name}"));
134+
if translation_path.exists() {
135+
let css_file = "theme/language-picker.css";
136+
let js_file = "theme/language-picker.js";
137+
138+
let css_path = translation_path.join(css_file);
139+
let js_path = translation_path.join(js_file);
140+
let po_path = translation_path.join("po");
141+
142+
let theme_dir = book_dir.join("theme");
143+
if !theme_dir.exists() {
144+
std::fs::create_dir(theme_dir)?;
145+
}
146+
let css_target = book_dir.join(css_file);
147+
let js_target = book_dir.join(js_file);
148+
std::fs::copy(css_path, &css_target)?;
149+
std::fs::copy(js_path, &js_target)?;
150+
cleanup.push(css_target);
151+
cleanup.push(js_target);
152+
153+
let css_file: toml::Value = css_file.into();
154+
let js_file: toml::Value = js_file.into();
155+
let po_path: toml::Value = po_path.to_string_lossy().as_ref().into();
156+
157+
if let Some(additional_css) = book.config.get_mut("output.html.additional-css") {
158+
additional_css.as_array_mut().unwrap().push(css_file.into());
159+
} else {
160+
book.config.set("output.html.additional-css", vec![css_file])?;
161+
}
162+
if let Some(additional_js) = book.config.get_mut("output.html.additional-js") {
163+
additional_js.as_array_mut().unwrap().push(js_file.into());
164+
} else {
165+
book.config.set("output.html.additional-js", vec![js_file])?;
166+
}
167+
book.config.set("preprocessor.gettext.po-dir", po_path)?;
168+
}
169+
}
170+
}
171+
124172
book.build()?;
125173

174+
for file in cleanup {
175+
std::fs::remove_file(file)?;
176+
}
177+
126178
Ok(())
127179
}
128180

0 commit comments

Comments
 (0)