Skip to content

Commit b8c6988

Browse files
committed
feat(types): introduce Empty translation variant for better handling of untranslated entries
- Added a new `Empty` variant to the `Translation` enum to represent empty translation entries. - Updated various parts of the codebase to handle this new variant, ensuring that empty translations are appropriately managed across different formats and functions. - This change improves the handling of untranslated items, allowing for clearer representation and processing of localization resources.
1 parent e6a7f1f commit b8c6988

12 files changed

Lines changed: 34 additions & 1 deletion

File tree

langcodec-cli/src/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn run_debug_command(input: String, lang: Option<String>, output: Option<Str
4444
for resource in &mut codec.resources {
4545
for entry in &mut resource.entries {
4646
entry.value = match &entry.value {
47+
Translation::Empty => Translation::Empty,
4748
Translation::Singular(v) => Translation::Singular(v.replace("\\n", "\n")),
4849
Translation::Plural(p) => Translation::Plural(Plural {
4950
id: p.id.clone(),

langcodec-cli/src/edit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ fn apply_set_to_file(
290290
let old = codec
291291
.find_entry(key, lang_ref)
292292
.map(|e| match &e.value {
293+
Translation::Empty => String::new(),
293294
Translation::Singular(s) => s.clone(),
294295
Translation::Plural(p) => p.id.clone(),
295296
})

langcodec-cli/src/view.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub fn print_view(codec: &Codec, lang_filter: &Option<String>, full: bool) {
6767
}
6868

6969
match &entry.value {
70+
langcodec::types::Translation::Empty => {
71+
println!(" Type: Empty");
72+
}
7073
langcodec::types::Translation::Singular(value) => {
7174
println!(" Type: Singular");
7275
if full {

langcodec-cli/tests/langcodec_output_simple_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ fn test_langcodec_output_structure() {
182182
assert!(!entry.id.is_empty(), "Entry should have id");
183183
// Value should be either Singular or Plural
184184
match &entry.value {
185+
langcodec::Translation::Empty => {}
185186
langcodec::Translation::Singular(s) => {
186187
assert!(!s.is_empty(), "Singular value should not be empty")
187188
}

langcodec/src/codec.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ impl Codec {
699699
for res in &self.resources {
700700
for entry in &res.entries {
701701
let sigs: Vec<Vec<String>> = match &entry.value {
702+
Translation::Empty => vec![],
702703
Translation::Singular(v) => vec![signature(v)],
703704
Translation::Plural(p) => p.forms.values().map(|v| signature(v)).collect(),
704705
};
@@ -766,6 +767,7 @@ impl Codec {
766767
for res in &self.resources {
767768
for entry in &res.entries {
768769
let sigs: Vec<Vec<String>> = match &entry.value {
770+
Translation::Empty => vec![],
769771
Translation::Singular(v) => vec![signature(v)],
770772
Translation::Plural(p) => p.forms.values().map(|v| signature(v)).collect(),
771773
};
@@ -826,6 +828,9 @@ impl Codec {
826828
for res in &mut self.resources {
827829
for entry in &mut res.entries {
828830
match &mut entry.value {
831+
Translation::Empty => {
832+
continue;
833+
}
829834
Translation::Singular(v) => {
830835
let nv = normalize_placeholders(v);
831836
*v = nv;

langcodec/src/converter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
260260
for res in &mut resources {
261261
for entry in &mut res.entries {
262262
match &mut entry.value {
263+
crate::types::Translation::Empty => {
264+
continue;
265+
}
263266
crate::types::Translation::Singular(v) => {
264267
*v = normalize_placeholders(v);
265268
}

langcodec/src/formats/android_strings.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl From<Resource> for Format {
135135
let mut plurals = Vec::new();
136136
for entry in value.entries {
137137
match entry.value {
138+
Translation::Empty => {} // Do nothing
138139
Translation::Singular(_) => strings.push(StringResource::from_entry(&entry)),
139140
Translation::Plural(p) => {
140141
let mut items: Vec<PluralItem> = p
@@ -250,6 +251,7 @@ impl StringResource {
250251
StringResource {
251252
name: entry.id.clone(),
252253
value: match &entry.value {
254+
Translation::Empty => String::new(),
253255
Translation::Singular(v) => v.clone(),
254256
Translation::Plural(_) => String::new(), // Plurals not supported in strings.xml
255257
},
@@ -469,7 +471,10 @@ World
469471
assert_eq!(entry.comment, None);
470472

471473
let entry = resource.find_entry("multiple_lines").unwrap();
472-
assert_eq!(entry.value, Translation::Singular("Hello\\n\\n\\nWorld\\n ".to_string()));
474+
assert_eq!(
475+
entry.value,
476+
Translation::Singular("Hello\\n\\n\\nWorld\\n ".to_string())
477+
);
473478
assert_eq!(entry.status, EntryStatus::Translated);
474479
assert_eq!(entry.comment, None);
475480

langcodec/src/formats/csv.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl TryFrom<Vec<Resource>> for Format {
208208
for resource in &resources {
209209
if let Some(entry) = resource.entries.iter().find(|e| e.id == record.key) {
210210
let value = match &entry.value {
211+
Translation::Empty => String::new(),
211212
Translation::Singular(v) => v.clone(),
212213
Translation::Plural(_) => String::new(), // Plurals not supported
213214
};

langcodec/src/formats/strings.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ impl TryFrom<Entry> for Pair {
502502
fn try_from(entry: Entry) -> Result<Self, Self::Error> {
503503
// Strings format only supports singular translations. Preserve the value verbatim.
504504
match entry.value {
505+
Translation::Empty => Ok(Pair {
506+
key: entry.id,
507+
value: String::new(),
508+
comment: entry.comment,
509+
}),
505510
Translation::Singular(value) => Ok(Pair {
506511
key: entry.id,
507512
value: crate::placeholder::to_ios_placeholders(&value),

langcodec/src/formats/tsv.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl TryFrom<Vec<Resource>> for Format {
211211
for resource in &resources {
212212
if let Some(entry) = resource.entries.iter().find(|e| e.id == record.key) {
213213
let value = match &entry.value {
214+
Translation::Empty => String::new(),
214215
Translation::Singular(v) => v.clone(),
215216
Translation::Plural(_) => String::new(), // Plurals not supported
216217
};

0 commit comments

Comments
 (0)