diff options
| author | Fuwn <[email protected]> | 2023-03-30 20:11:20 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-03-30 20:11:20 -0700 |
| commit | a63b2174be4bb897d717034ed42e2b7676645deb (patch) | |
| tree | cbd89ccadc6fbaef751fd6f6021050ee9929ee6c /src/modules/router/translate/module.rs | |
| parent | feat(translate): minimize translation passes (diff) | |
| download | locus-a63b2174be4bb897d717034ed42e2b7676645deb.tar.xz locus-a63b2174be4bb897d717034ed42e2b7676645deb.zip | |
feat(translation): add translation header
Diffstat (limited to 'src/modules/router/translate/module.rs')
| -rw-r--r-- | src/modules/router/translate/module.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/modules/router/translate/module.rs b/src/modules/router/translate/module.rs new file mode 100644 index 0000000..8c3b162 --- /dev/null +++ b/src/modules/router/translate/module.rs @@ -0,0 +1,87 @@ +// This file is part of Locus <https://github.com/gemrest/locus>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +use super::deepl::translate; +use crate::modules::router::translate::deepl::language_code_to_language_name; + +pub fn module( + cc: &windmark::returnable::CallbackContext<'_>, + content: &mut String, +) { + if let Some(language) = + windmark::utilities::queries_from_url(cc.url).get("translate") + { + let lines = content + .lines() + .map(ToString::to_string) + .collect::<Vec<String>>(); + let mut preformatted = false; + let mut saved_lines = std::collections::HashMap::new(); + let mut fully_translated = Vec::new(); + + for (i, line) in &mut lines.iter().enumerate() { + if line == "```" { + preformatted = !preformatted; + } + + if line.starts_with("=>") { + if let Some(germ::ast::Node::Link { + to, + text, + }) = germ::ast::Ast::from_string(line).inner().get(0) + { + saved_lines.insert( + i, + format!( + "=> {to}?translate={language}{}", + text.clone().map_or_else( + || "".to_string(), + |text| { format!(" {}", translate(&text, language).text()) } + ) + ), + ); + } + } else if preformatted { + saved_lines.insert(i, line.to_string()); + } + } + + let translated = translate(content, language); + + for (i, line) in translated.text().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 = format!( + "This content has been translated from {} to {}.\n\n=> {} View \ + Original\n\n{}", + language_code_to_language_name( + &translated.detected_source_language().to_lowercase() + ), + language_code_to_language_name(&language.to_string().to_lowercase()), + cc.url + .to_string() + .replace(&format!("translate={language}"), ""), + fully_translated.join("\n") + ); + } +} |