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
use reqwest;
use serde_json::Value;
/// Translates text from one language to another.
///
/// # Arguments
///
/// * `text` - The text to be translated.
/// * `from` - The language code of the source language.
/// * `to` - The language code of the target language.
///
/// # Examples
///
///
/// ```
/// let translated_text = translate("Bonjour", "fr", "en").await.unwrap();
/// assert_eq!(translated_text, "Hello");
/// ```
pub async fn translate(text: &str, from: &str, to: &str) -> Result<String, Box<dyn std::error::Error>> {
let url = format!(
"https://translate.googleapis.com/translate_a/single?client=gtx&sl={}&tl={}&dt=t&q={}",
from, to, text
);
let response = reqwest::get(&url).await?.text().await?;
let translated_text: String = serde_json::from_str::<Value>(&response)?[0][0][0].as_str().unwrap().to_string();
Ok(translated_text)
}
/// Translates text to English from the detected language.
///
/// # Arguments
///
/// * `text` - The text to be translated.
///
/// # Examples
///
/// ```
/// let translated_text = translate_to_english("Bonjour").await.unwrap();
/// assert_eq!(translated_text, "Hello");
/// ```
pub async fn translate_to_english(text: &str) -> Result<String, Box<dyn std::error::Error>> {
let from = "auto";
let to = "en";
let url = format!(
"https://translate.googleapis.com/translate_a/single?client=gtx&sl={}&tl={}&dt=t&q={}",
from, to, text
);
let response = reqwest::get(&url).await?.text().await?;
let translated_text: String = serde_json::from_str::<Value>(&response)?[0][0][0].as_str().unwrap().to_string();
Ok(translated_text)
}
/// Translates text from English to the specified language.
///
/// # Arguments
///
/// * `text` - The text to be translated.
/// * `to` - The language code of the target language.
///
/// # Examples
///
/// ```
/// let translated_text = translate_from_english("Hello", "fr").await.unwrap();
/// assert_eq!(translated_text, "Bonjour");
/// ```
pub async fn translate_from_english(text: &str, to: &str) -> Result<String, Box<dyn std::error::Error>> {
let url = format!(
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl={}&dt=t&q={}",
to, text
);
let response = reqwest::get(&url).await?.text().await?;
let translated_text: String = serde_json::from_str::<Value>(&response)?[0][0][0].as_str().unwrap().to_string();
Ok(translated_text)
}