-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate to the bootstrap process of rust-lang/rust #193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -24,63 +24,14 @@ | |||||||
//! PO files. If the PO file is not found, you'll get the untranslated | ||||||||
//! book. | ||||||||
|
||||||||
use anyhow::{anyhow, Context}; | ||||||||
use mdbook::preprocess::{CmdPreprocessor, PreprocessorContext}; | ||||||||
use mdbook_i18n_helpers::gettext::{add_stripped_summary_translations, translate_book}; | ||||||||
use polib::catalog::Catalog; | ||||||||
use polib::po_file; | ||||||||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor}; | ||||||||
use mdbook_i18n_helpers::preprocessors::Gettext; | ||||||||
use semver::{Version, VersionReq}; | ||||||||
use std::path::PathBuf; | ||||||||
use std::{io, process}; | ||||||||
|
||||||||
/// Check whether the book should be transalted. | ||||||||
/// | ||||||||
/// The book should be translated if: | ||||||||
/// * `book.language` is defined in mdbook config | ||||||||
/// * Corresponding {language}.po defined | ||||||||
fn should_translate(ctx: &PreprocessorContext) -> bool { | ||||||||
// Translation is a no-op when the target language is not set | ||||||||
if ctx.config.book.language.is_none() { | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
// Nothing to do if PO file is missing. | ||||||||
get_catalog_path(ctx) | ||||||||
.map(|path| path.try_exists().unwrap_or(false)) | ||||||||
.unwrap_or(false) | ||||||||
} | ||||||||
|
||||||||
/// Compute the path of the Catalog file. | ||||||||
fn get_catalog_path(ctx: &PreprocessorContext) -> anyhow::Result<PathBuf> { | ||||||||
let language = ctx | ||||||||
.config | ||||||||
.book | ||||||||
.language | ||||||||
.as_ref() | ||||||||
.ok_or_else(|| anyhow!("Language is not provided"))?; | ||||||||
|
||||||||
let cfg = ctx | ||||||||
.config | ||||||||
.get_preprocessor("gettext") | ||||||||
.ok_or_else(|| anyhow!("Could not read preprocessor.gettext configuration"))?; | ||||||||
let po_dir = cfg.get("po-dir").and_then(|v| v.as_str()).unwrap_or("po"); | ||||||||
Ok(ctx.root.join(po_dir).join(format!("{language}.po"))) | ||||||||
} | ||||||||
|
||||||||
/// Load the catalog with translation strings. | ||||||||
fn load_catalog(ctx: &PreprocessorContext) -> anyhow::Result<Catalog> { | ||||||||
let path = get_catalog_path(ctx)?; | ||||||||
|
||||||||
let catalog = po_file::parse(&path) | ||||||||
.map_err(|err| anyhow!("{err}")) | ||||||||
.with_context(|| format!("Could not parse {path:?} as PO file"))?; | ||||||||
|
||||||||
Ok(catalog) | ||||||||
} | ||||||||
|
||||||||
/// Execute main logic by this mdbook preprocessor. | ||||||||
fn preprocess() -> anyhow::Result<()> { | ||||||||
let (ctx, mut book) = CmdPreprocessor::parse_input(io::stdin())?; | ||||||||
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?; | ||||||||
let book_version = Version::parse(&ctx.mdbook_version)?; | ||||||||
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?; | ||||||||
#[allow(clippy::print_stderr)] | ||||||||
|
@@ -93,11 +44,8 @@ fn preprocess() -> anyhow::Result<()> { | |||||||
); | ||||||||
} | ||||||||
|
||||||||
if should_translate(&ctx) { | ||||||||
let mut catalog = load_catalog(&ctx)?; | ||||||||
add_stripped_summary_translations(&mut catalog); | ||||||||
translate_book(&catalog, &mut book); | ||||||||
} | ||||||||
let gettext = Gettext; | ||||||||
let book = gettext.run(&ctx, book)?; | ||||||||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like above, I think we don't need the variable:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @dalance, can you try out this change and see if it compiles? If so, please add it to the PR. Thanks! |
||||||||
|
||||||||
serde_json::to_writer(io::stdout(), &book)?; | ||||||||
|
||||||||
|
@@ -107,10 +55,14 @@ fn preprocess() -> anyhow::Result<()> { | |||||||
fn main() -> anyhow::Result<()> { | ||||||||
if std::env::args().len() == 3 { | ||||||||
assert_eq!(std::env::args().nth(1).as_deref(), Some("supports")); | ||||||||
if let Some("xgettext") = std::env::args().nth(2).as_deref() { | ||||||||
process::exit(1) | ||||||||
if let Some(renderer) = std::env::args().nth(2).as_deref() { | ||||||||
let gettext = Gettext; | ||||||||
if gettext.supports_renderer(renderer) { | ||||||||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this work as well:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't change the definition of I can define another static method like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see now! The syntax I used above would only work if the Then let's merge this PR as it is — and then later change the code here to create a single |
||||||||
process::exit(0) | ||||||||
} else { | ||||||||
process::exit(1) | ||||||||
} | ||||||||
} else { | ||||||||
// Signal that we support all other renderers. | ||||||||
process::exit(0); | ||||||||
} | ||||||||
} | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod gettext; | ||
pub use gettext::Gettext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use crate::gettext::{add_stripped_summary_translations, translate_book}; | ||
use anyhow::{anyhow, Context}; | ||
use mdbook::preprocess::{Preprocessor, PreprocessorContext}; | ||
use polib::catalog::Catalog; | ||
use polib::po_file; | ||
use std::path::PathBuf; | ||
|
||
/// Check whether the book should be transalted. | ||
/// | ||
/// The book should be translated if: | ||
/// * `book.language` is defined in mdbook config | ||
/// * Corresponding {language}.po defined | ||
fn should_translate(ctx: &PreprocessorContext) -> bool { | ||
// Translation is a no-op when the target language is not set | ||
if ctx.config.book.language.is_none() { | ||
return false; | ||
} | ||
|
||
// Nothing to do if PO file is missing. | ||
get_catalog_path(ctx) | ||
.map(|path| path.try_exists().unwrap_or(false)) | ||
.unwrap_or(false) | ||
} | ||
|
||
/// Compute the path of the Catalog file. | ||
fn get_catalog_path(ctx: &PreprocessorContext) -> anyhow::Result<PathBuf> { | ||
let language = ctx | ||
.config | ||
.book | ||
.language | ||
.as_ref() | ||
.ok_or_else(|| anyhow!("Language is not provided"))?; | ||
|
||
let cfg = ctx | ||
.config | ||
.get_preprocessor("gettext") | ||
.ok_or_else(|| anyhow!("Could not read preprocessor.gettext configuration"))?; | ||
let po_dir = cfg.get("po-dir").and_then(|v| v.as_str()).unwrap_or("po"); | ||
Ok(ctx.root.join(po_dir).join(format!("{language}.po"))) | ||
} | ||
|
||
/// Load the catalog with translation strings. | ||
fn load_catalog(ctx: &PreprocessorContext) -> anyhow::Result<Catalog> { | ||
let path = get_catalog_path(ctx)?; | ||
|
||
let catalog = po_file::parse(&path) | ||
.map_err(|err| anyhow!("{err}")) | ||
.with_context(|| format!("Could not parse {path:?} as PO file"))?; | ||
|
||
Ok(catalog) | ||
} | ||
|
||
/// Preprocessor for gettext | ||
pub struct Gettext; | ||
|
||
impl Preprocessor for Gettext { | ||
fn name(&self) -> &str { | ||
"gettext" | ||
} | ||
|
||
fn run( | ||
&self, | ||
ctx: &PreprocessorContext, | ||
mut book: mdbook::book::Book, | ||
) -> anyhow::Result<mdbook::book::Book> { | ||
if should_translate(ctx) { | ||
let mut catalog = load_catalog(ctx)?; | ||
add_stripped_summary_translations(&mut catalog); | ||
translate_book(&catalog, &mut book); | ||
} | ||
Ok(book) | ||
} | ||
|
||
fn supports_renderer(&self, renderer: &str) -> bool { | ||
renderer != "xgettext" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might get a PR from the dependabot, but we can then tell it to ignore the 1.10 releases.