Update po/pot dumper to add name to msgctxt

This commit is contained in:
2025-12-30 10:22:25 +08:00
parent d8ef082645
commit 7670570a88

View File

@@ -3,7 +3,7 @@
//! See [spec](https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html) //! See [spec](https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html)
use crate::types::*; use crate::types::*;
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug)] #[derive(Debug)]
@@ -269,17 +269,26 @@ impl PoDumper {
msgid_plural: None, msgid_plural: None,
msgstr: MsgStr::Single(Self::gen_start_str(encoding)), msgstr: MsgStr::Single(Self::gen_start_str(encoding)),
}); });
let mut added_messages: HashMap<&String, usize> = HashMap::new(); let mut added: HashSet<&String> = HashSet::new();
let mut added_messages: HashMap<(&String, &Option<String>), usize> = HashMap::new();
for entry in entries { for entry in entries {
let count = added_messages.get(&entry.message).map(|&s| s).unwrap_or(0); let count = added_messages
.get(&(&entry.message, &entry.name))
.map(|&s| s)
.unwrap_or(0);
let inadded = added.contains(&entry.message);
self.add_entry(PoEntry { self.add_entry(PoEntry {
comments: entry comments: entry
.name .name
.as_ref() .as_ref()
.map(|name| vec![Comment::Translator(format!("NAME: {}", name))]) .map(|name| vec![Comment::Translator(format!("NAME: {}", name))])
.unwrap_or_default(), .unwrap_or_default(),
msgctxt: if count > 0 { msgctxt: if count > 0 || inadded {
Some(format!("{}", count)) Some(format!(
"{}{}",
entry.name.as_ref().map(|s| s.as_str()).unwrap_or(""),
count
))
} else { } else {
None None
}, },
@@ -287,7 +296,10 @@ impl PoDumper {
msgid_plural: None, msgid_plural: None,
msgstr: MsgStr::Single(String::new()), msgstr: MsgStr::Single(String::new()),
}); });
added_messages.insert(&entry.message, count + 1); added_messages.insert((&entry.message, &entry.name), count + 1);
if !inadded {
added.insert(&entry.message);
}
} }
let mut result = String::new(); let mut result = String::new();
for line in &self.entries { for line in &self.entries {