aboutsummaryrefslogtreecommitdiff
path: root/src/modules/router/translate/deepl.rs
blob: b1767e8b6db63d95b1f90ce38c9e305f1650f533 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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,
) -> Result<DeepL, serde_json::Error> {
  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()
  })?;

  Ok(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),
  }
}