aboutsummaryrefslogtreecommitdiff
path: root/src/modules/router/translate/module.rs
blob: 8c3b16226335d14b21319d25c67fef5e727f0114 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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")
    );
  }
}