Skip to content
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

Split POT file depending on specified depth #77

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::FromStr;
use libfuzzer_sys::fuzz_target;
use mdbook::renderer::RenderContext;
use mdbook::Config;
use mdbook_i18n_helpers::xgettext::create_catalog;
use mdbook_i18n_helpers::xgettext::create_catalogs;
use mdbook_i18n_helpers_fuzz::{create_book, BookItem};

fuzz_target!(|inputs: (&str, Vec<BookItem>)| {
Expand All @@ -16,5 +16,5 @@ fuzz_target!(|inputs: (&str, Vec<BookItem>)| {

let ctx = RenderContext::new(PathBuf::new(), book, Config::from_str("").unwrap(), "");

let _ = create_catalog(&ctx, |_| Ok(summary.to_string()));
let _ = create_catalogs(&ctx, |_| Ok(summary.to_string()));
});
12 changes: 11 additions & 1 deletion i18n-helpers/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,22 @@ To extract the original text and generate a `messages.pot` file, you run
`mdbook` with the `mdbook-xgettext` renderer:

```shell
MDBOOK_OUTPUT='{"xgettext": {"pot-file": "messages.pot"}}' \
MDBOOK_OUTPUT='{"xgettext": {}}' \
mdbook build -d po
```

You will find the generated POT file as `po/messages.pot`.

To extract the text into smaller `.pot` files based on the text's Markdown
outline, use the `depth` parameter. For a `depth` of `1`, the `.pot` lines will
be separated into a file for each section or chapter title. Use greater values
to split the `.pot` file further.

```shell
MDBOOK_OUTPUT='{"xgettext": {"depth": "1"}}' \
mdbook build -d po/messages
```

### Initialize a New Translation

To start a new translation for a fictional `xx` locale, first generate the
Expand Down
32 changes: 16 additions & 16 deletions i18n-helpers/src/bin/mdbook-xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
//! This program works like `xgettext`, meaning it will extract
//! translatable strings from your book. The strings are saved in a
//! GNU Gettext `messages.pot` file in your build directory (typically
//! `po/messages.pot`).
//! `po/messages.pot`). When the `depth` parameter is included, a new
//! directory will contain the template files split based on the tiers
//! of Chapter nesting.

use anyhow::{anyhow, Context};
use anyhow::Context;
use mdbook::renderer::RenderContext;
use mdbook_i18n_helpers::xgettext::create_catalog;
use mdbook_i18n_helpers::xgettext::create_catalogs;
use std::{fs, io};

fn main() -> anyhow::Result<()> {
let ctx = RenderContext::from_json(&mut io::stdin()).context("Parsing stdin")?;
let cfg = ctx
.config
.get_renderer("xgettext")
.ok_or_else(|| anyhow!("Could not read output.xgettext configuration"))?;
let path = cfg
.get("pot-file")
.ok_or_else(|| anyhow!("Missing output.xgettext.pot-file config value"))?
.as_str()
.ok_or_else(|| anyhow!("Expected a string for output.xgettext.pot-file"))?;
antoniolinhart marked this conversation as resolved.
Show resolved Hide resolved
fs::create_dir_all(&ctx.destination)
.with_context(|| format!("Could not create {}", ctx.destination.display()))?;
let output_path = ctx.destination.join(path);
let catalog = create_catalog(&ctx, std::fs::read_to_string).context("Extracting messages")?;
polib::po_file::write(&catalog, &output_path)
.with_context(|| format!("Writing messages to {}", output_path.display()))?;
let catalogs = create_catalogs(&ctx, std::fs::read_to_string).context("Extracting messages")?;

// Create a template file for each entry with the content from the respective catalog.
for (file_path, catalog) in catalogs {
let directory_path = file_path.parent().unwrap();
fs::create_dir_all(directory_path)
.with_context(|| format!("Could not create {}", directory_path.display()))?;

polib::po_file::write(&catalog, &file_path)
.with_context(|| format!("Writing messages to {}", file_path.display()))?;
}

Ok(())
}
Loading