-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use mdbook::renderer::RenderContext; | ||
use serde::Deserialize; | ||
use std::collections::BTreeMap; | ||
use std::io; | ||
|
||
#[derive(Deserialize)] | ||
struct I18nConfiguration { | ||
languages: BTreeMap<String, String>, | ||
default_language: Option<String>, | ||
|
||
#[serde(default)] | ||
translate_all_languages: bool, | ||
#[serde(default)] | ||
move_translations_to_html_directory: bool, | ||
} | ||
|
||
fn main() { | ||
let mut stdin = io::stdin(); | ||
|
||
// Get the configs | ||
let ctx = RenderContext::from_json(&mut stdin).unwrap(); | ||
let i18n_config: I18nConfiguration = ctx | ||
.config | ||
.get_deserialized_opt("output.i18n-helpers") | ||
.unwrap() | ||
.unwrap(); | ||
|
||
if !i18n_config.translate_all_languages { | ||
return; | ||
} | ||
|
||
let mut mdbook = mdbook::MDBook::load(&ctx.root).expect("Failed to load book"); | ||
// Overwrite with current values from stdin. | ||
mdbook.book = ctx.book.clone(); | ||
mdbook.config = ctx.config.clone(); | ||
mdbook.root = ctx.root.clone(); | ||
|
||
let book_config = mdbook | ||
.config | ||
.get_mut("output.i18n-helpers") | ||
.expect("No output.i18n-helpers config in book.toml"); | ||
// Set translate_all_languages to false for nested builds to prevent infinite recursion. | ||
book_config | ||
.as_table_mut() | ||
.expect("output.i18n-helpers config in book.toml is not a table") | ||
.insert( | ||
String::from("translate_all_languages"), | ||
toml::Value::Boolean(false), | ||
); | ||
|
||
let output_directory = ctx.destination.parent().unwrap(); | ||
let default_language = &i18n_config.default_language; | ||
|
||
for language in i18n_config.languages.keys() { | ||
// Skip current language and default language. | ||
if Some(language) == ctx.config.book.language.as_ref() { | ||
continue; | ||
} | ||
if let Some(default_language) = default_language { | ||
if default_language == language { | ||
continue; | ||
} | ||
} | ||
let translation_path = output_directory.join(language); | ||
|
||
// Book doesn't implement clone, so we just mutate in place. | ||
mdbook.config.book.language = Some(language.clone()); | ||
mdbook.config.book.multilingual = true; | ||
mdbook.config.build.build_dir = translation_path; | ||
mdbook.build().unwrap_or_else(|_| panic!("Failed to build translation for language: {}", | ||
language)); | ||
if i18n_config.move_translations_to_html_directory { | ||
std::fs::rename( | ||
output_directory.join(language).join("html"), | ||
output_directory.join("html").join(language), | ||
) | ||
.unwrap_or_else(|_| panic!("Failed to move translation for language {language} to output directory")); | ||
} | ||
} | ||
} |