diff options
| author | Fuwn <[email protected]> | 2023-03-30 17:58:11 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-03-30 17:58:11 -0700 |
| commit | 0544d2503603452b13da14c99a3a1a8aea373a4a (patch) | |
| tree | ae1a64166df67088622cab60a4cc8e694f9f0518 /src/modules | |
| parent | refactor(router): move ticker and translate to modules (diff) | |
| download | locus-0544d2503603452b13da14c99a3a1a8aea373a4a.tar.xz locus-0544d2503603452b13da14c99a3a1a8aea373a4a.zip | |
feat(translate): minimize translation passes
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/router/translate.rs | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/modules/router/translate.rs b/src/modules/router/translate.rs index 99f4423..fe94af4 100644 --- a/src/modules/router/translate.rs +++ b/src/modules/router/translate.rs @@ -25,13 +25,15 @@ pub fn module( if let Some(language) = windmark::utilities::queries_from_url(cc.url).get("translate") { - let mut lines = content + let lines = content .lines() .map(ToString::to_string) .collect::<Vec<String>>(); - let mut preformatted = true; + let mut preformatted = false; + let mut saved_lines = std::collections::HashMap::new(); + let mut fully_translated = Vec::new(); - for line in &mut lines { + for (i, line) in &mut lines.iter().enumerate() { if line == "```" { preformatted = !preformatted; } @@ -42,19 +44,32 @@ pub fn module( text, }) = germ::ast::Ast::from_string(line).inner().get(0) { - *line = format!( - "=> {to}?translate={language}{}", - text.clone().map_or_else( - || "".to_string(), - |text| { format!(" {}", translate(&text, language).text()) } - ) + saved_lines.insert( + i, + format!( + "=> {to}?translate={language}{}", + text.clone().map_or_else( + || "".to_string(), + |text| { format!(" {}", translate(&text, language).text()) } + ) + ), ); } - } else if !line.starts_with("=>") && preformatted && !line.is_empty() { - *line = translate(line, language).text(); + } else if preformatted { + saved_lines.insert(i, line.to_string()); } } - *content = lines.join("\n"); + let translated = translate(content, language).text(); + + for (i, line) in translated.lines().enumerate() { + if saved_lines.contains_key(&i) { + fully_translated.push(saved_lines.get(&i).unwrap().to_string()); + } else { + fully_translated.push(line.to_string()); + } + } + + *content = fully_translated.join("\n"); } } |