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 | |
| 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')
| -rw-r--r-- | src/modules/router.rs | 1 | ||||
| -rw-r--r-- | src/modules/router/deepl.rs | 50 | ||||
| -rw-r--r-- | src/modules/router/translate.rs | 59 | ||||
| -rw-r--r-- | src/modules/router/translate/deepl.rs | 122 | ||||
| -rw-r--r-- | src/modules/router/translate/module.rs | 87 | ||||
| -rw-r--r-- | src/modules/static.rs | 4 |
6 files changed, 214 insertions, 109 deletions
diff --git a/src/modules/router.rs b/src/modules/router.rs index 881c0fd..46e72ec 100644 --- a/src/modules/router.rs +++ b/src/modules/router.rs @@ -16,7 +16,6 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -mod deepl; mod module; mod ticker; mod translate; diff --git a/src/modules/router/deepl.rs b/src/modules/router/deepl.rs deleted file mode 100644 index d4e210f..0000000 --- a/src/modules/router/deepl.rs +++ /dev/null @@ -1,50 +0,0 @@ -// 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 - -#[derive(serde::Deserialize)] -pub struct DeepL { - translations: Vec<Translation>, -} - -impl DeepL { - pub fn text(&self) -> String { self.translations[0].text.clone() } -} - -#[derive(serde::Deserialize)] -struct Translation { - pub text: String, -} - -pub fn translate(text: &str, language: &str) -> DeepL { - let deepl_response: DeepL = serde_json::from_str( - &reqwest::blocking::Client::new() - .post("https://api-free.deepl.com/v2/translate") - .header( - "Authorization", - "DeepL-Auth-Key 182c8c21-730e-f99d-8f2e-61e9cc861faa:fx", - ) - .form(&[("text", text), ("target_lang", language)]) - .send() - .unwrap() - .text() - .unwrap(), - ) - .unwrap(); - - deepl_response -} diff --git a/src/modules/router/translate.rs b/src/modules/router/translate.rs index fe94af4..8f24a9b 100644 --- a/src/modules/router/translate.rs +++ b/src/modules/router/translate.rs @@ -16,60 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use crate::modules::router::deepl::translate; +mod deepl; +mod module; -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).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"); - } -} +pub use module::module; diff --git a/src/modules/router/translate/deepl.rs b/src/modules/router/translate/deepl.rs new file mode 100644 index 0000000..00e7538 --- /dev/null +++ b/src/modules/router/translate/deepl.rs @@ -0,0 +1,122 @@ +// 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 + +#[derive(serde::Deserialize)] +pub struct DeepL { + translations: Vec<Translation>, +} + +impl DeepL { + pub fn text(&self) -> String { self.translations[0].text.clone() } + + pub fn detected_source_language(&self) -> String { + self.translations[0].detected_source_language.clone() + } +} + +#[derive(serde::Deserialize)] +struct Translation { + pub text: String, + pub detected_source_language: String, +} + +pub fn translate(text: &str, language: &str) -> DeepL { + let deepl_response: DeepL = serde_json::from_str( + &reqwest::blocking::Client::new() + .post("https://api-free.deepl.com/v2/translate") + .header( + "Authorization", + "DeepL-Auth-Key 182c8c21-730e-f99d-8f2e-61e9cc861faa:fx", + ) + .form(&[("text", text), ("target_lang", language)]) + .send() + .unwrap() + .text() + .unwrap(), + ) + .unwrap(); + + deepl_response +} + +pub fn language_code_to_language_name(language_code: &str) -> String { + match language_code { + "am" => "Amharic".to_string(), + "ar" => "Arabic".to_string(), + "eu" => "Basque".to_string(), + "bn" => "Bengali".to_string(), + // "en-GB" => "English (UK)".to_string(), + "en-gb" => "English (UK)".to_string(), + // "pt-BR" => "Portuguese (Brazil)".to_string(), + "pt-br" => "Portuguese (Brazil)".to_string(), + "bg" => "Bulgarian".to_string(), + "ca" => "Catalan".to_string(), + "chr" => "Cherokee".to_string(), + "hr" => "Croatian".to_string(), + "cs" => "Czech".to_string(), + "da" => "Danish".to_string(), + "nl" => "Dutch".to_string(), + "en" => "English (US)".to_string(), + "et" => "Estonian".to_string(), + "fil" => "Filipino".to_string(), + "fi" => "Finnish".to_string(), + "fr" => "French".to_string(), + "de" => "German".to_string(), + "el" => "Greek".to_string(), + "gu" => "Gujarati".to_string(), + "iw" => "Hebrew".to_string(), + "hi" => "Hindi".to_string(), + "hu" => "Hungarian".to_string(), + "is" => "Icelandic".to_string(), + "id" => "Indonesian".to_string(), + "it" => "Italian".to_string(), + "ja" => "Japanese".to_string(), + "kn" => "Kannada".to_string(), + "ko" => "Korean".to_string(), + "lv" => "Latvian".to_string(), + "lt" => "Lithuanian".to_string(), + "ms" => "Malay".to_string(), + "ml" => "Malayalam".to_string(), + "mr" => "Marathi".to_string(), + "no" => "Norwegian".to_string(), + "pl" => "Polish".to_string(), + // "pt-PT" => "Portuguese (Portugal)".to_string(), + "pt-pt" => "Portuguese (Portugal)".to_string(), + "ro" => "Romanian".to_string(), + "ru" => "Russian".to_string(), + "sr" => "Serbian".to_string(), + // "zh-CN" => "Chinese (PRC)".to_string(), + "zh-cn" => "Chinese (PRC)".to_string(), + "sk" => "Slovak".to_string(), + "sl" => "Slovenian".to_string(), + "es" => "Spanish".to_string(), + "sw" => "Swahili".to_string(), + "sv" => "Swedish".to_string(), + "ta" => "Tamil".to_string(), + "te" => "Telugu".to_string(), + "th" => "Thai".to_string(), + // "zh-TW" => "Chinese (Taiwan)".to_string(), + "zh-tw" => "Chinese (Taiwan)".to_string(), + "tr" => "Turkish".to_string(), + "ur" => "Urdu".to_string(), + "uk" => "Ukrainian".to_string(), + "vi" => "Vietnamese".to_string(), + "cy" => "Welsh".to_string(), + _ => format!("Unknown ({})", language_code), + } +} 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") + ); + } +} diff --git a/src/modules/static.rs b/src/modules/static.rs index 7197ac2..d2535df 100644 --- a/src/modules/static.rs +++ b/src/modules/static.rs @@ -62,9 +62,9 @@ pub fn module(router: &mut windmark::Router) { "stocks_telegram" ), ( - "/translate", + "/translations", "Translate a page to your language of choice!", - "translate" + "translations" ) ); } |