langcodec_cli/transformers/
json_array_language_map.rs1use std::collections::HashMap;
2
3use langcodec::{Entry, EntryStatus, Metadata, Resource, Translation};
4
5pub fn transform(input: String) -> Result<Vec<Resource>, String> {
23 let file_content = match std::fs::read_to_string(&input) {
24 Ok(content) => content,
25 Err(e) => return Err(format!("Error reading file {}: {}", input, e)),
26 };
27
28 let json_array: Vec<HashMap<String, String>> = match serde_json::from_str(&file_content) {
30 Ok(arr) => arr,
31 Err(e) => {
32 return Err(format!(
33 "Error parsing JSON array from {}: {}. Expected format: [{{\"key\": \"hello\", \"en\": \"Hello\", \"fr\": \"Bonjour\"}}]",
34 input, e
35 ));
36 }
37 };
38
39 if json_array.is_empty() {
40 return Err("Error: JSON array is empty".to_string());
41 }
42
43 let mut resources = Vec::new();
44 let mut language_resources: HashMap<String, Vec<Entry>> = HashMap::new();
45
46 for (index, entry) in json_array.iter().enumerate() {
47 if entry.is_empty() {
48 continue;
49 }
50
51 let localization_key = entry
54 .get("key")
55 .unwrap_or(entry.get("en").unwrap_or(entry.iter().next().unwrap().1));
56
57 for (lang_code, value) in entry.iter() {
58 if lang_code == "key" {
60 continue;
61 }
62
63 let mut entry_custom = HashMap::new();
64 entry_custom.insert("extraction_state".to_string(), "manual".to_string());
65 entry_custom.insert("array_index".to_string(), index.to_string());
66
67 let resource_entry = Entry {
68 id: localization_key.clone(),
69 value: Translation::Singular(value.clone()),
70 status: EntryStatus::NeedsReview,
71 comment: None,
72 custom: entry_custom,
73 };
74
75 let entries = language_resources.entry(lang_code.clone()).or_default();
77
78 if let Some(existing_index) = entries.iter().position(|e| e.id == *localization_key) {
80 entries[existing_index] = resource_entry;
82 } else {
83 entries.push(resource_entry);
85 }
86 }
87 }
88
89 for (lang_code, entries) in language_resources {
91 let mut metadata_custom: HashMap<String, String> = HashMap::new();
92 metadata_custom.insert("source_language".to_string(), "en".to_string());
93 metadata_custom.insert("version".to_string(), "1.0".to_string());
94 metadata_custom.insert("format".to_string(), "JSONArrayLanguageMap".to_string());
95
96 let metadata = Metadata {
97 language: lang_code.clone(),
98 domain: "".to_string(),
99 custom: metadata_custom,
100 };
101
102 resources.push(Resource { metadata, entries });
103 }
104
105 Ok(resources)
106}