1use crate::locale::{Locale, LocaleRegistry};
24use std::collections::HashMap;
25
26#[derive(Debug, Clone)]
31pub struct KeywordTranslator {
32 forward: HashMap<&'static str, &'static str>,
34 pub target_locale: Locale,
36}
37
38impl KeywordTranslator {
39 pub fn for_locale(registry: &LocaleRegistry, target: Locale) -> Option<Self> {
41 let table = registry.table_for(target)?;
42 let mut forward = HashMap::new();
44 for (locale_kw, canonical_kw) in &table.keywords {
45 forward.insert(*canonical_kw, *locale_kw);
46 }
47 Some(Self {
48 forward,
49 target_locale: target,
50 })
51 }
52
53 pub fn translate_keyword<'a>(&self, canonical: &'a str) -> &'a str {
55 self.forward.get(canonical).copied().unwrap_or(canonical)
56 }
57
58 pub fn translate_line(&self, line: &str) -> String {
62 let mut result = String::with_capacity(line.len());
63 let mut in_string = false;
64 let mut in_line_comment = false;
65 let mut current_token = String::new();
66
67 let flush_token = |token: &str, result: &mut String| {
68 if !token.is_empty() {
69 let translated = self.translate_keyword(token);
71 result.push_str(translated);
72 }
73 };
74
75 let chars: Vec<char> = line.chars().collect();
76 let mut i = 0;
77 while i < chars.len() {
78 let c = chars[i];
79
80 if in_line_comment {
81 result.push(c);
82 i += 1;
83 continue;
84 }
85
86 if in_string {
87 result.push(c);
88 if c == '"' {
89 in_string = false;
90 }
91 i += 1;
92 continue;
93 }
94
95 if c == '/' && i + 1 < chars.len() && chars[i + 1] == '/' {
97 flush_token(¤t_token, &mut result);
98 current_token.clear();
99 in_line_comment = true;
100 result.push(c);
101 i += 1;
102 continue;
103 }
104
105 if c == '"' {
107 flush_token(¤t_token, &mut result);
108 current_token.clear();
109 in_string = true;
110 result.push(c);
111 i += 1;
112 continue;
113 }
114
115 if c.is_alphanumeric() || c == '_' {
117 current_token.push(c);
118 } else {
119 flush_token(¤t_token, &mut result);
120 current_token.clear();
121 result.push(c);
122 }
123 i += 1;
124 }
125 flush_token(¤t_token, &mut result);
126 result
127 }
128
129 pub fn translate_source(&self, source: &str) -> String {
131 source
132 .lines()
133 .map(|line| self.translate_line(line))
134 .collect::<Vec<_>>()
135 .join("\n")
136 }
137}
138
139pub fn translate_source(registry: &LocaleRegistry, source: &str, target: Locale) -> Option<String> {
144 let translator = KeywordTranslator::for_locale(registry, target)?;
145 Some(translator.translate_source(source))
146}
147
148#[derive(Debug, Clone, PartialEq)]
153pub struct Tier2Label {
154 pub iri: String,
156 pub locale: String,
158 pub label: String,
160 pub description: Option<String>,
162}
163
164impl Tier2Label {
165 pub fn new(iri: &str, locale: &str, label: &str) -> Self {
167 Self {
168 iri: iri.into(),
169 locale: locale.into(),
170 label: label.into(),
171 description: None,
172 }
173 }
174
175 pub fn with_description(mut self, desc: &str) -> Self {
177 self.description = Some(desc.into());
178 self
179 }
180}
181
182#[derive(Debug, Clone, Default)]
184pub struct Tier2Registry {
185 labels: HashMap<String, Vec<Tier2Label>>,
186}
187
188impl Tier2Registry {
189 pub fn new() -> Self {
191 Self::default()
192 }
193
194 pub fn register(&mut self, label: Tier2Label) -> &mut Self {
196 self.labels
197 .entry(label.iri.clone())
198 .or_default()
199 .push(label);
200 self
201 }
202
203 pub fn labels_for(&self, iri: &str) -> &[Tier2Label] {
205 self.labels.get(iri).map(|v| v.as_slice()).unwrap_or(&[])
206 }
207
208 pub fn label_in_locale(&self, iri: &str, locale: &str) -> Option<&Tier2Label> {
210 self.labels.get(iri)?.iter().find(|l| l.locale == locale)
211 }
212
213 pub fn labels_in_locale(&self, locale: &str) -> Vec<&Tier2Label> {
215 self.labels
216 .values()
217 .flat_map(|v| v.iter())
218 .filter(|l| l.locale == locale)
219 .collect()
220 }
221
222 pub fn iri_count(&self) -> usize {
224 self.labels.len()
225 }
226
227 pub fn label_count(&self) -> usize {
229 self.labels.values().map(|v| v.len()).sum()
230 }
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236
237 fn make_registry() -> LocaleRegistry {
238 LocaleRegistry::with_en_and_zh()
239 }
240
241 #[test]
244 fn t38_translate_keyword_en_to_zh() {
245 let registry = make_registry();
246 let translator = KeywordTranslator::for_locale(®istry, Locale::ZH).unwrap();
247 assert_eq!(translator.translate_keyword("fn"), "函数");
249 }
250
251 #[test]
252 fn t38_translate_keyword_unknown_keeps_canonical() {
253 let registry = make_registry();
254 let translator = KeywordTranslator::for_locale(®istry, Locale::ZH).unwrap();
255 assert_eq!(translator.translate_keyword("foobar"), "foobar");
257 }
258
259 #[test]
260 fn t38_translate_line_replaces_keywords() {
261 let registry = make_registry();
262 let translator = KeywordTranslator::for_locale(®istry, Locale::ZH).unwrap();
263 let translated = translator.translate_line("fn main() {");
264 assert!(translated.contains("函数"));
265 assert!(!translated.contains("fn "));
266 }
267
268 #[test]
269 fn t38_translate_line_preserves_strings() {
270 let registry = make_registry();
271 let translator = KeywordTranslator::for_locale(®istry, Locale::ZH).unwrap();
272 let translated = translator.translate_line(r#" return "fn is a word";"#);
273 assert!(translated.contains("\"fn is a word\""));
275 }
276
277 #[test]
278 fn t38_translate_line_preserves_comments() {
279 let registry = make_registry();
280 let translator = KeywordTranslator::for_locale(®istry, Locale::ZH).unwrap();
281 let translated = translator.translate_line("// fn is a keyword");
282 assert!(translated.contains("// fn is a keyword"));
284 }
285
286 #[test]
287 fn t38_translate_source_multiline() {
288 let registry = make_registry();
289 let source = "fn main() {\n return 0;\n}";
290 let translated = translate_source(®istry, source, Locale::ZH).unwrap();
291 assert!(translated.contains("函数"));
292 assert!(translated.contains("main"));
293 }
294
295 #[test]
296 fn t38_translate_source_en_to_en_is_identity() {
297 let registry = make_registry();
298 let source = "fn main() { return 0; }";
299 let translator = KeywordTranslator::for_locale(®istry, Locale::EN);
302 match translator {
305 Some(t) => {
306 let translated = t.translate_source(source);
307 assert_eq!(translated, source);
308 }
309 None => {
310 assert_eq!(source, source);
312 }
313 }
314 }
315
316 #[test]
319 fn t39_tier2_label_basic() {
320 let l = Tier2Label::new("clinic:hasCondition", "en", "has condition");
321 assert_eq!(l.iri, "clinic:hasCondition");
322 assert_eq!(l.locale, "en");
323 assert_eq!(l.label, "has condition");
324 assert!(l.description.is_none());
325 }
326
327 #[test]
328 fn t39_tier2_label_with_description() {
329 let l = Tier2Label::new("clinic:hasCondition", "en", "has condition")
330 .with_description("The patient has this medical condition.");
331 assert_eq!(
332 l.description,
333 Some("The patient has this medical condition.".into())
334 );
335 }
336
337 #[test]
338 fn t39_registry_register_and_query() {
339 let mut reg = Tier2Registry::new();
340 reg.register(Tier2Label::new(
341 "clinic:hasCondition",
342 "en",
343 "has condition",
344 ));
345 reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
346 reg.register(Tier2Label::new("clinic:hasPatient", "en", "has patient"));
347
348 assert_eq!(reg.iri_count(), 2);
349 assert_eq!(reg.label_count(), 3);
350
351 let labels = reg.labels_for("clinic:hasCondition");
352 assert_eq!(labels.len(), 2);
353 }
354
355 #[test]
356 fn t39_registry_label_in_locale() {
357 let mut reg = Tier2Registry::new();
358 reg.register(Tier2Label::new(
359 "clinic:hasCondition",
360 "en",
361 "has condition",
362 ));
363 reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
364
365 let en = reg.label_in_locale("clinic:hasCondition", "en").unwrap();
366 assert_eq!(en.label, "has condition");
367
368 let zh = reg.label_in_locale("clinic:hasCondition", "zh").unwrap();
369 assert_eq!(zh.label, "有病情");
370 }
371
372 #[test]
373 fn t39_registry_labels_in_locale() {
374 let mut reg = Tier2Registry::new();
375 reg.register(Tier2Label::new(
376 "clinic:hasCondition",
377 "en",
378 "has condition",
379 ));
380 reg.register(Tier2Label::new("clinic:hasPatient", "en", "has patient"));
381 reg.register(Tier2Label::new("clinic:hasCondition", "zh", "有病情"));
382
383 let en_labels = reg.labels_in_locale("en");
384 assert_eq!(en_labels.len(), 2);
385 let zh_labels = reg.labels_in_locale("zh");
386 assert_eq!(zh_labels.len(), 1);
387 }
388
389 #[test]
390 fn t39_registry_missing_iri() {
391 let reg = Tier2Registry::new();
392 assert!(reg.labels_for("unknown:iri").is_empty());
393 assert!(reg.label_in_locale("unknown:iri", "en").is_none());
394 }
395
396 #[test]
397 fn t39_registry_empty() {
398 let reg = Tier2Registry::new();
399 assert_eq!(reg.iri_count(), 0);
400 assert_eq!(reg.label_count(), 0);
401 }
402
403 #[test]
404 fn test_multilingual_all_locales_translation() {
405 let registry = LocaleRegistry::with_all_locales();
406 let src = "fn calculate() {\n let x = 10;\n if x > 5 {\n return true;\n }\n return false;\n}";
407
408 for loc in [
409 Locale::ZH,
410 Locale::ES,
411 Locale::JA,
412 Locale::AR,
413 Locale::HI,
414 Locale::FR,
415 Locale::DE,
416 ] {
417 let translated = translate_source(®istry, src, loc).unwrap();
418 assert!(
419 !translated.is_empty(),
420 "translation for {} should not be empty",
421 loc
422 );
423 let with_locale = format!("locale {};\n{translated}", loc.code());
425 let parsed = crate::parse_program(&with_locale);
426 assert!(
427 parsed.is_ok(),
428 "failed to parse translated source for {}: {:?}",
429 loc,
430 parsed.err()
431 );
432 }
433 }
434}