From 3dfa35d7e9b3a578257777593f5c0e0d4270ff99 Mon Sep 17 00:00:00 2001 From: Willow Carretero Chavez Date: Fri, 19 Jul 2024 19:52:59 +0000 Subject: [PATCH 1/3] Respect granularity for sub-chapter messages --- i18n-helpers/src/xgettext.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i18n-helpers/src/xgettext.rs b/i18n-helpers/src/xgettext.rs index cb8b533..af5d266 100644 --- a/i18n-helpers/src/xgettext.rs +++ b/i18n-helpers/src/xgettext.rs @@ -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); } } From 5cddc82ca3a2f54d4ba82c38366e883b8909a946 Mon Sep 17 00:00:00 2001 From: Willow Carretero Chavez Date: Mon, 22 Jul 2024 18:35:52 +0000 Subject: [PATCH 2/3] Add test --- i18n-helpers/src/xgettext.rs | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/i18n-helpers/src/xgettext.rs b/i18n-helpers/src/xgettext.rs index af5d266..5c41d93 100644 --- a/i18n-helpers/src/xgettext.rs +++ b/i18n-helpers/src/xgettext.rs @@ -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::>(); + + 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(&[ From e2f439ac69472c19ad54fc96bf27a84ed3ffb6c7 Mon Sep 17 00:00:00 2001 From: Darkhan Kubigenov Date: Tue, 23 Jul 2024 09:31:15 +0100 Subject: [PATCH 3/3] Run `cargo fmt` --- i18n-helpers/src/xgettext.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18n-helpers/src/xgettext.rs b/i18n-helpers/src/xgettext.rs index 5c41d93..28c68cd 100644 --- a/i18n-helpers/src/xgettext.rs +++ b/i18n-helpers/src/xgettext.rs @@ -768,7 +768,7 @@ mod tests { Ok(()) } - + #[test] fn test_split_catalog_nested_directories_depth_1() -> anyhow::Result<()> { let (ctx, _tmp) = create_render_context(&[