From a63b2174be4bb897d717034ed42e2b7676645deb Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 30 Mar 2023 20:11:20 -0700 Subject: feat(translation): add translation header --- src/modules/router/translate/module.rs | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/modules/router/translate/module.rs (limited to 'src/modules/router/translate/module.rs') 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 . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// 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::>(); + 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") + ); + } +} -- cgit v1.2.3