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

Make xgettext respect granularity for sub-chapter messages #215

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Changes from 2 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
68 changes: 66 additions & 2 deletions i18n-helpers/src/xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ where
.entry(destination.clone())
.or_insert_with(|| Catalog::new(generate_catalog_metadata(ctx)));

let source = ctx.config.book.src.join(&source);
let path = ctx.config.book.src.join(&source);
for (lineno, extracted) in extract_messages(&content) {
let msgid = extracted.message;
let source = format!("{}:{}", source.display(), lineno);
let source = build_source(&path, lineno, granularity);
add_message(catalog, &msgid, &source, &extracted.comment);
}
}
Expand Down Expand Up @@ -704,7 +704,71 @@ mod tests {

Ok(())
}
#[test]

fn test_create_catalog_nested_directories_respects_granularity() -> anyhow::Result<()> {
let (ctx, _tmp) = create_render_context(&[
(
"book.toml",
"[book]\n\
[output.xgettext]\n\
granularity=0",
),
(
"src/SUMMARY.md",
"- [The Foo Chapter](foo.md)\n\
\t- [The Bar Section](foo/bar.md)\n\
\t\t- [The Baz Subsection](foo/bar/baz.md)",
),
(
"src/foo.md",
"# How to Foo\n\
\n\
The first paragraph about Foo.\n",
),
(
"src/foo/bar.md",
"# How to Bar\n\
\n\
The first paragraph about Bar.\n",
),
(
"src/foo/bar/baz.md",
"# How to Baz\n\
\n\
The first paragraph about Baz.\n",
),
])?;

let catalogs = create_catalogs(&ctx, std::fs::read_to_string)?;
let catalog = &catalogs[&default_template_file()];

for msg in catalog.messages() {
assert!(!msg.is_translated());
}

let expected_message_tuples = vec![
("src/SUMMARY.md", "The Foo Chapter"),
("src/SUMMARY.md", "The Bar Section"),
("src/SUMMARY.md", "The Baz Subsection"),
("src/foo.md", "How to Foo"),
("src/foo.md", "The first paragraph about Foo."),
("src/foo/bar.md", "How to Bar"),
("src/foo/bar.md", "The first paragraph about Bar."),
("src/foo/bar/baz.md", "How to Baz"),
("src/foo/bar/baz.md", "The first paragraph about Baz."),
];

let message_tuples = catalog
.messages()
.map(|msg| (msg.source(), msg.msgid()))
.collect::<Vec<(&str, &str)>>();

assert_eq!(expected_message_tuples, message_tuples);

Ok(())
}

#[test]
fn test_split_catalog_nested_directories_depth_1() -> anyhow::Result<()> {
let (ctx, _tmp) = create_render_context(&[
Expand Down
Loading