#[derive(serde::Deserialize)] pub struct DeepL { translations: Vec, } 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 { 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})"), } }