1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::{
collections::BTreeMap,
fs::File,
io::{self, BufReader},
path::Path,
};
use displaydoc::Display;
use quick_xml::{events::Event as XmlEvent, Error as XmlError, Reader as XmlReader};
use thiserror::Error;
use super::common::exact::{expect_attribute, expect_end, expect_start, expect_text, Error};
#[derive(Debug, Display, Error)]
pub enum LocaleError {
Io(#[from] io::Error),
Xml(#[from] Error),
}
impl From<XmlError> for LocaleError {
fn from(e: XmlError) -> Self {
Self::Xml(Error::Xml(e))
}
}
#[derive(Debug, Default)]
pub struct LocaleNode {
pub value: Option<String>,
pub int_children: BTreeMap<u32, LocaleNode>,
pub str_children: BTreeMap<String, LocaleNode>,
}
impl LocaleNode {
pub fn get_keys(&self) -> BTreeMap<String, String> {
let mut keys = BTreeMap::new();
for (key, value) in &self.str_children {
value.add_keys(&mut keys, key.clone());
}
for (key, value) in &self.int_children {
value.add_keys(&mut keys, key.to_string());
}
keys
}
fn add_keys(&self, keys: &mut BTreeMap<String, String>, prefix: String) {
for (key, value) in &self.str_children {
let inner = format!("{}_{}", prefix, key);
value.add_keys(keys, inner);
}
for (key, value) in &self.int_children {
let inner = format!("{}_{}", prefix, key);
value.add_keys(keys, inner);
}
if let Some(v) = &self.value {
keys.insert(prefix, v.clone());
}
}
}
pub fn load_locale(path: &Path) -> Result<LocaleNode, LocaleError> {
let file = File::open(path)?;
let file = BufReader::new(file);
let mut root = LocaleNode {
value: None,
int_children: BTreeMap::new(),
str_children: BTreeMap::new(),
};
let mut reader = XmlReader::from_reader(file);
reader.trim_text(true);
let mut buf = Vec::new();
if let Ok(XmlEvent::Decl(_)) = reader.read_event(&mut buf) {}
buf.clear();
let _ = expect_start("localization", &mut reader, &mut buf)?;
buf.clear();
let e_locales = expect_start("locales", &mut reader, &mut buf)?;
let locale_count = expect_attribute("count", &reader, &e_locales)?;
buf.clear();
for _ in 0..locale_count {
let _ = expect_start("locale", &mut reader, &mut buf)?;
buf.clear();
let _locale = expect_text(&mut reader, &mut buf)?;
buf.clear();
let _ = expect_end("locale", &mut reader, &mut buf)?;
buf.clear();
}
let _ = expect_end("locales", &mut reader, &mut buf)?;
buf.clear();
let e_locales = expect_start("phrases", &mut reader, &mut buf)?;
let phrase_count = expect_attribute("count", &reader, &e_locales)?;
buf.clear();
for _ in 0..phrase_count {
let e_phrase = expect_start("phrase", &mut reader, &mut buf)?;
let id: String = expect_attribute("id", &reader, &e_phrase)?;
buf.clear();
let mut translation = None;
loop {
let event = reader.read_event(&mut buf)?;
let e_translation = match event {
XmlEvent::End(e) => {
if e.name() == b"phrase" {
break;
} else {
let name_str = reader.decode(e.name());
return Err(LocaleError::Xml(Error::ExpectedEndTag(
String::from("phrase"),
name_str.into_owned(),
)));
}
}
XmlEvent::Start(e) => {
if e.name() == b"translation" {
e
} else {
let name_str = reader.decode(e.name());
return Err(LocaleError::Xml(Error::ExpectedEndTag(
String::from("translation"),
name_str.into_owned(),
)));
}
}
_ => panic!(),
};
let locale: String = expect_attribute("locale", &reader, &e_translation)?;
buf.clear();
let trans = expect_text(&mut reader, &mut buf)?;
if &locale == "en_US" {
translation = Some(trans);
}
buf.clear();
let _ = expect_end("translation", &mut reader, &mut buf)?;
buf.clear();
}
let mut node = &mut root;
for comp in id.split('_') {
if let Ok(num) = comp.parse::<u32>() {
node = node.int_children.entry(num).or_default();
} else {
node = node.str_children.entry(comp.to_owned()).or_default();
}
}
if let Some(translation) = translation {
node.value = Some(translation);
}
}
let _ = expect_end("phrases", &mut reader, &mut buf)?;
buf.clear();
Ok(root)
}