diff --git a/guide/src/en/SUMMARY.md b/guide/src/en/SUMMARY.md index 37e9fca0..c42184cc 100644 --- a/guide/src/en/SUMMARY.md +++ b/guide/src/en/SUMMARY.md @@ -21,6 +21,7 @@ - [Syntax highlighting](format/theme/syntax-highlighting.md) - [Editor](format/theme/editor.md) - [MathJax Support](format/mathjax.md) + - [Localization](format/localization.md) - [mdBook-specific features](format/mdbook.md) - [Continuous Integration](continuous-integration.md) - [For Developers](for_developers/README.md) diff --git a/guide/src/en/format/configuration/localization.md b/guide/src/en/format/configuration/localization.md new file mode 100644 index 00000000..d0c0355b --- /dev/null +++ b/guide/src/en/format/configuration/localization.md @@ -0,0 +1 @@ +# Localization diff --git a/src/book/mod.rs b/src/book/mod.rs index a30c13aa..7403ff04 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -215,26 +215,35 @@ impl MDBook { /// Run the entire build process for a particular [`Renderer`]. pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> { - let preprocess_ctx = PreprocessorContext::new( - self.root.clone(), - self.build_opts.clone(), - self.config.clone(), - renderer.name().to_string(), - ); - let preprocessed_books = match &self.book { LoadedBook::Localized(ref books) => { let mut new_books = HashMap::new(); - for (ident, book) in books.0.iter() { + for (language_ident, book) in books.0.iter() { + let preprocess_ctx = PreprocessorContext::new( + self.root.clone(), + Some(language_ident.clone()), + self.build_opts.clone(), + self.config.clone(), + renderer.name().to_string(), + ); + let preprocessed_book = self.preprocess(&preprocess_ctx, renderer, book.clone())?; - new_books.insert(ident.clone(), preprocessed_book); + new_books.insert(language_ident.clone(), preprocessed_book); } LoadedBook::Localized(LocalizedBooks(new_books)) } LoadedBook::Single(ref book) => { + let preprocess_ctx = PreprocessorContext::new( + self.root.clone(), + None, + self.build_opts.clone(), + self.config.clone(), + renderer.name().to_string(), + ); + LoadedBook::Single(self.preprocess(&preprocess_ctx, renderer, book.clone())?) } }; @@ -273,10 +282,17 @@ impl MDBook { self } - fn test_book(&self, book: &Book, temp_dir: &TempDir, library_args: &Vec<&str>) -> Result<()> { + fn test_book( + &self, + book: &Book, + temp_dir: &TempDir, + library_args: &Vec<&str>, + language_ident: Option, + ) -> Result<()> { // FIXME: Is "test" the proper renderer name to use here? let preprocess_context = PreprocessorContext::new( self.root.clone(), + language_ident, self.build_opts.clone(), self.config.clone(), "test".to_string(), diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 24d7b75b..a77914bf 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -50,10 +50,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> { trigger_on_change(&book, |paths, book_dir| { info!("Files changed: {:?}\nBuilding book...\n", paths); - let result = MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| { - update_config(&mut b); - b.build() - }); + let result = + MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| { + update_config(&mut b); + b.build() + }); if let Err(e) = result { error!("Unable to build the book"); diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 8d96a011..9dfdc651 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -42,7 +42,11 @@ impl Preprocessor for LinkPreprocessor { } fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result { - let src_dir = ctx.source_dir(); + let src_dir = ctx + .config + .get_localized_src_path(ctx.language_ident.as_ref()) + .unwrap(); + let src_dir = ctx.root.join(src_dir); book.for_each_mut(|section: &mut BookItem| { if let BookItem::Chapter(ref mut ch) = *section { diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 0fdeaa92..278b1808 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -23,6 +23,9 @@ use std::path::PathBuf; pub struct PreprocessorContext { /// The location of the book directory on disk. pub root: PathBuf, + /// The language of the book being built. Is only `Some` if the book is part + /// of a multilingual build output. + pub language_ident: Option, /// The build options passed from the frontend. pub build_opts: BuildOpts, /// The book configuration (`book.toml`). @@ -41,12 +44,14 @@ impl PreprocessorContext { /// Create a new `PreprocessorContext`. pub(crate) fn new( root: PathBuf, + language_ident: Option, build_opts: BuildOpts, config: Config, renderer: String, ) -> Self { PreprocessorContext { root, + language_ident, build_opts, config, renderer,