From c0df41f5e77e7ce5edde07c574d763176a875d97 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 9 Jun 2023 17:44:55 +0200 Subject: [PATCH] slint-tr-extractor: add -j option to join to an existing file --- tools/tr-extractor/main.rs | 40 +++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tools/tr-extractor/main.rs b/tools/tr-extractor/main.rs index eec3204a254..100025649b2 100644 --- a/tools/tr-extractor/main.rs +++ b/tools/tr-extractor/main.rs @@ -41,6 +41,9 @@ struct Cli { // help = "Set the reporting address for msgid bugs. This is the email address or URL to which the translators shall report bugs in the untranslated strings" // )] // msgid_bugs_address: Option, + #[arg(long = "join-existing", short = 'j')] + /// Join messages with existing file + join_existing: bool, } fn main() -> std::io::Result<()> { @@ -50,24 +53,29 @@ fn main() -> std::io::Result<()> { format!("{}.po", args.domain.as_ref().map(String::as_str).unwrap_or("messages")).into() }); - let package = args.package_name.as_ref().map(|x| x.as_ref()).unwrap_or("PACKAGE"); - let version = args.package_version.as_ref().map(|x| x.as_ref()).unwrap_or("VERSION"); - let metadata = polib::metadata::CatalogMetadata { - project_id_version: format!("{package} {version}",), - pot_creation_date: chrono::Utc::now().format("%Y-%m-%d %H:%M%z").to_string(), - po_revision_date: "YEAR-MO-DA HO:MI+ZONE".into(), - last_translator: "FULL NAME ".into(), - language_team: "LANGUAGE ".into(), - mime_version: "1.0".into(), - content_type: "text/plain; charset=UTF-8".into(), - content_transfer_encoding: "8bit".into(), - language: String::new(), - plural_rules: Default::default(), - // "Report-Msgid-Bugs-To: {address}" addess = output_details.bugs_address + let mut messages = if args.join_existing { + polib::po_file::parse(&output) + .map_err(|x| std::io::Error::new(std::io::ErrorKind::Other, x))? + } else { + let package = args.package_name.as_ref().map(|x| x.as_ref()).unwrap_or("PACKAGE"); + let version = args.package_version.as_ref().map(|x| x.as_ref()).unwrap_or("VERSION"); + let metadata = polib::metadata::CatalogMetadata { + project_id_version: format!("{package} {version}",), + pot_creation_date: chrono::Utc::now().format("%Y-%m-%d %H:%M%z").to_string(), + po_revision_date: "YEAR-MO-DA HO:MI+ZONE".into(), + last_translator: "FULL NAME ".into(), + language_team: "LANGUAGE ".into(), + mime_version: "1.0".into(), + content_type: "text/plain; charset=UTF-8".into(), + content_transfer_encoding: "8bit".into(), + language: String::new(), + plural_rules: Default::default(), + // "Report-Msgid-Bugs-To: {address}" addess = output_details.bugs_address + }; + + Messages::new(metadata) }; - let mut messages = Messages::new(metadata); - for path in args.paths { process_file(path, &mut messages)? }