diff --git a/i18n-helpers/src/xgettext.rs b/i18n-helpers/src/xgettext.rs index cb8b5331..28c68cd5 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); } } @@ -704,6 +704,70 @@ 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<()> {