-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mrtryhard/v0.2.0
V0.2.0
- Loading branch information
Showing
7 changed files
with
255 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## Unreleased | ||
|
||
## Added | ||
|
||
## Changed | ||
|
||
## Fixed | ||
|
||
## [0.2.0] - 2024-01-01 | ||
|
||
Completion of the [second milestone](https://github.com/mrtryhard/qt-ts-tools/milestone/2). This introduces the `extract` and lighter binary. | ||
|
||
### Added | ||
|
||
- Extraction mechanism to extract only relevant translation types. [#16](https://github.com/mrtryhard/qt-ts-tools/issues/16) | ||
|
||
### Changed | ||
|
||
- Reduced release binary size [#19](https://github.com/mrtryhard/qt-ts-tools/issues/19) | ||
- Updated Serde and Clap dependencies [#20](https://github.com/mrtryhard/qt-ts-tools/issues/20) | ||
|
||
### Fixed | ||
|
||
## [0.1.0] - 2024-01-30 | ||
|
||
Introduction of `qt-ts-tools`. This completes the first [milestone](https://github.com/mrtryhard/qt-ts-tools/milestone/1?closed=1). | ||
|
||
### Added | ||
|
||
- Sort mechanism to sort translation files by location and contexts. [#3](https://github.com/mrtryhard/qt-ts-tools/issues/3) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::ts; | ||
use crate::ts::{TSNode, TranslationType}; | ||
use clap::Args; | ||
|
||
#[derive(Args)] | ||
pub struct ExtractArgs { | ||
pub input_path: String, | ||
#[arg(short('t'), long, value_enum, num_args = 1..)] | ||
pub translation_type: Vec<TranslationTypeArg>, | ||
#[arg(short, long)] | ||
pub output_path: Option<String>, | ||
} | ||
|
||
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)] | ||
pub enum TranslationTypeArg { | ||
Obsolete, | ||
Unfinished, | ||
Vanished, | ||
} | ||
|
||
/// Filters the translation file to keep only the messages containing unfinished translations. | ||
pub fn extract_main(args: &ExtractArgs) -> 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 wanted_types = args | ||
.translation_type | ||
.iter() | ||
.map(to_translation_type) | ||
.collect(); | ||
retain_ts_node(&mut ts_node, &wanted_types); | ||
ts::write_to_output(&args.output_path, &ts_node) | ||
} | ||
Err(e) => Err(format!( | ||
"Could not parse input file \"{}\". Error: {e:?}.", | ||
args.input_path | ||
)), | ||
} | ||
} | ||
Err(e) => Err(format!( | ||
"Could not open or parse input file \"{}\". Error: {e:?}", | ||
args.input_path | ||
)), | ||
} | ||
} | ||
|
||
fn to_translation_type(value: &TranslationTypeArg) -> TranslationType { | ||
match value { | ||
TranslationTypeArg::Obsolete => TranslationType::Obsolete, | ||
TranslationTypeArg::Unfinished => TranslationType::Unfinished, | ||
TranslationTypeArg::Vanished => TranslationType::Vanished, | ||
} | ||
} | ||
|
||
/// Keep only the desired translation type from the node (if it matches one in `wanted_types`). | ||
fn retain_ts_node(ts_node: &mut TSNode, wanted_types: &Vec<TranslationType>) { | ||
ts_node.contexts.retain_mut(|context| { | ||
context.messages.retain(|message| { | ||
message.translation.as_ref().is_some_and(|translation| { | ||
translation | ||
.translation_type | ||
.as_ref() | ||
.is_some_and(|translation_type| wanted_types.contains(&translation_type)) | ||
}) | ||
}); | ||
|
||
context.messages.len() > 0 | ||
}); | ||
} | ||
|
||
#[cfg(test)] | ||
mod extract_test { | ||
use super::*; | ||
use quick_xml; | ||
|
||
#[test] | ||
fn test_extract_ts_node() { | ||
let reader_nosort = quick_xml::Reader::from_file("./test_data/example_key_de.xml") | ||
.expect("Couldn't open example_unfinished test file"); | ||
let mut data: TSNode = | ||
quick_xml::de::from_reader(reader_nosort.into_inner()).expect("Parsable"); | ||
|
||
let types = vec![TranslationType::Obsolete]; | ||
retain_ts_node(&mut data, &types); | ||
|
||
assert_eq!(data.contexts[0].messages.len(), 3); | ||
assert_eq!( | ||
data.contexts[0].messages[0].source.as_ref().unwrap(), | ||
"Shift+K" | ||
); | ||
assert_eq!( | ||
data.contexts[0].messages[1].source.as_ref().unwrap(), | ||
"Ctrl+K" | ||
); | ||
assert_eq!( | ||
data.contexts[0].messages[2].source.as_ref().unwrap(), | ||
"Alt+K" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.