-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
use reqwest; | ||
use serde_json::Value; | ||
|
||
pub fn translate(text: &str, from: &str, to: &str) -> String { | ||
// placeholder | ||
text.to_string() | ||
text.to_string() | ||
} | ||
|
||
pub fn translate_to_english(text: &str) -> String { | ||
// placeholder | ||
text.to_string() | ||
} | ||
|
||
pub fn translate_from_english(text: &str, to: &str) -> String { | ||
// placeholder | ||
text.to_string() | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters