Skip to content

Commit

Permalink
fixes #56: Finalize strip feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtryhard committed Jun 22, 2024
1 parent 836c056 commit 46a3437
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 0 deletions.
142 changes: 142 additions & 0 deletions src/commands/strip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::locale::{tr, tr_args};
use crate::ts;
use crate::ts::{TSNode, TranslationType};
use clap::{ArgAction, Args};
use tracing::debug;

#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
pub enum TranslationTypeArg {
Obsolete,
Unfinished,
Vanished,
}

impl From<TranslationTypeArg> for TranslationType {
fn from(value: TranslationTypeArg) -> Self {
match value {
TranslationTypeArg::Obsolete => TranslationType::Obsolete,
TranslationTypeArg::Unfinished => TranslationType::Unfinished,
TranslationTypeArg::Vanished => TranslationType::Vanished,
}
}
}

#[derive(Args)]
#[command(disable_help_flag = true)]
pub struct StripArgs {
/// File path to sort translations from.
#[arg(help = tr("cli-strip-input"), help_heading = tr("cli-headers-arguments"))]
pub input_path: String,
/// Translation type list to strip from input.
#[arg(short('t'), long, value_enum, num_args = 1.., help = tr("cli-strip-translation-type"), help_heading = tr("cli-headers-arguments"))]
pub translation_type: Vec<TranslationTypeArg>,
/// If specified, will produce output in a file at designated location instead of stdout.
#[arg(short, long, help = tr("cli-strip-output"), help_heading = tr("cli-headers-options"))]
pub output_path: Option<String>,
#[arg(short, long, action = ArgAction::Help, help = tr("cli-help"), help_heading = tr("cli-headers-options"))]
pub help: Option<bool>,
}

pub fn strip_main(args: &StripArgs) -> Result<(), String> {
match quick_xml::Reader::from_file(&args.input_path) {
Ok(file) => {
let nodes: Result<TSNode, _> = quick_xml::de::from_reader(file.into_inner());
match nodes {
Ok(mut ts_node) => {
let s: Vec<TranslationType> = args
.translation_type
.iter()
.map(|arg| arg.clone().into())
.collect();

strip_nodes(&mut ts_node, &s);
ts::write_to_output(&args.output_path, &ts_node)
}
Err(e) => Err(tr_args(
"strip-parse-error",
[
("file", args.input_path.as_str().into()),
("error", e.to_string().into()),
]
.into(),
)),
}
}
Err(e) => Err(tr_args(
"error-open-or-parse",
[
("file", args.input_path.as_str().into()),
("error", e.to_string().into()),
]
.into(),
)),
}
}

fn strip_nodes(nodes: &mut TSNode, translation_type_filter: &[TranslationType]) {
let mut count = 0;
nodes.contexts.iter_mut().for_each(|context| {
context.messages.iter_mut().for_each(|message| {
if let Some(translation) = &mut message.translation.as_ref() {
if let Some(translation_type) = translation.translation_type.clone() {
if translation_type_filter.contains(&translation_type) {
debug!(
"Stripping translation {:?} from message `{}`",
&translation.translation_simple,
&message
.source
.as_ref()
.unwrap_or(&"Unknown source text".to_owned())
);
message.translation = None;
count += 1;
}
}
}
});
});

nodes.messages.iter_mut().for_each(|message| {
if let Some(translation) = &mut message.translation.as_ref() {
if let Some(translation_type) = translation.translation_type.clone() {
if translation_type_filter.contains(&translation_type) {
debug!(
"Stripping translation {:?} from message `{}`",
&translation.translation_simple,
&message
.source
.as_ref()
.unwrap_or(&"Unknown source text".to_owned())
);
message.translation = None;
count += 1;
}
}
}
});

debug!("Stripped {count} translation tags");
}

#[cfg(test)]
mod strip_test {
use super::*;

#[test]
fn test_strip() {
let reader_unstripped = quick_xml::Reader::from_file("./test_data/example_strip.xml")
.expect("Couldn't open example_strip test file");
let reader_stripped =
quick_xml::Reader::from_file("./test_data/example_strip_stripped.xml")
.expect("Couldn't open example_strip_stripped test file");
let mut data: TSNode =
quick_xml::de::from_reader(reader_unstripped.into_inner()).expect("Parsable");
let data_stripped: TSNode =
quick_xml::de::from_reader(reader_stripped.into_inner()).expect("Parsable");

let types = vec![TranslationType::Obsolete];
strip_nodes(&mut data, &types);

assert_eq!(data, data_stripped);
}
}
77 changes: 77 additions & 0 deletions test_data/example_strip.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="1.1" language="de">
<context>
<name>tst_QKeySequence</name>
<message>
<location filename="tst_qkeysequence.cpp" line="369"/>
<source>Shift+K</source>
<translation type="obsolete">Umschalt+K</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="370"/>
<source>Ctrl+K</source>
<translation type="obsolete">Strg+K</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="150"/>
<location filename="tst_qkeysequence.cpp" line="371"/>
<source>Alt+K</source>
<translation type="obsolete">Alt+K</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="391"/>
<source>Shift++</source>
<translation>Umschalt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="392"/>
<source>Ctrl++</source>
<translation>Strg++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="393"/>
<source>Alt++</source>
<translation>Alt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="394"/>
<source>Meta++</source>
<translation>Meta++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="401"/>
<source>Shift+,, Shift++</source>
<translation>Umschalt+,, Umschalt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="402"/>
<source>Shift+,, Ctrl++</source>
<translation>Umschalt+,, Strg++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="403"/>
<source>Shift+,, Alt++</source>
<translation>Umschalt+,, Alt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation>Umschalt+,, Meta++</translation>
</message>
</context>
<message>
<location filename="tst_nostrip.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation>Umschalt+,, Meta++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation type="obsolete">Umschalt+,, Meta++</translation>
</message>
<message>
<location filename="tst_nostrip2.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation type="vanished">Umschalt+,, Meta++</translation>
</message>
</TS>
73 changes: 73 additions & 0 deletions test_data/example_strip_stripped.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="1.1" language="de">
<context>
<name>tst_QKeySequence</name>
<message>
<location filename="tst_qkeysequence.cpp" line="369"/>
<source>Shift+K</source>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="370"/>
<source>Ctrl+K</source>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="150"/>
<location filename="tst_qkeysequence.cpp" line="371"/>
<source>Alt+K</source>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="391"/>
<source>Shift++</source>
<translation>Umschalt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="392"/>
<source>Ctrl++</source>
<translation>Strg++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="393"/>
<source>Alt++</source>
<translation>Alt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="394"/>
<source>Meta++</source>
<translation>Meta++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="401"/>
<source>Shift+,, Shift++</source>
<translation>Umschalt+,, Umschalt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="402"/>
<source>Shift+,, Ctrl++</source>
<translation>Umschalt+,, Strg++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="403"/>
<source>Shift+,, Alt++</source>
<translation>Umschalt+,, Alt++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation>Umschalt+,, Meta++</translation>
</message>
</context>
<message>
<location filename="tst_nostrip.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation>Umschalt+,, Meta++</translation>
</message>
<message>
<location filename="tst_qkeysequence.cpp" line="404"/>
<source>Shift+,, Meta++</source>
</message>
<message>
<location filename="tst_nostrip2.cpp" line="404"/>
<source>Shift+,, Meta++</source>
<translation type="vanished">Umschalt+,, Meta++</translation>
</message>
</TS>

0 comments on commit 46a3437

Please sign in to comment.