Skip to content

Commit

Permalink
Deduplicate sources after all messages are added to the catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
zachcmadsen committed Feb 7, 2024
1 parent ba57994 commit 994ac6e
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions i18n-helpers/src/xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{anyhow, Context};
use mdbook::renderer::RenderContext;
use mdbook::BookItem;
use polib::catalog::Catalog;
use polib::message::Message;
use polib::message::{Message, MessageMutView, MessageView};
use polib::metadata::CatalogMetadata;
use pulldown_cmark::{Event, Tag};

Expand All @@ -41,13 +41,7 @@ fn strip_link(text: &str) -> String {

fn add_message(catalog: &mut Catalog, msgid: &str, source: &str) {
let sources = match catalog.find_message(None, msgid, None) {
Some(msg) => {
if msg.source().contains(source) {
return;
}

wrap_sources(&format!("{}\n{}", msg.source(), source))
}
Some(msg) => format!("{}\n{}", msg.source(), source),
None => String::from(source),
};
let message = Message::build_singular()
Expand Down Expand Up @@ -82,6 +76,16 @@ fn build_source<P: AsRef<path::Path>>(path: P, lineno: usize, granularity: usize
}
}

fn dedup_sources(catalog: &mut Catalog) {
for mut message in catalog.messages_mut() {
let mut lines: Vec<&str> = message.source().lines().collect();
lines.dedup();

let wrapped_source = wrap_sources(&lines.join("\n"));
*message.source_mut() = wrapped_source;
}
}

/// Build catalog from RenderContext
///
/// # Arguments
Expand Down Expand Up @@ -150,6 +154,8 @@ where
}
}

dedup_sources(&mut catalog);

Ok(catalog)
}

Expand Down

0 comments on commit 994ac6e

Please sign in to comment.