pub struct DeepLApi { /* private fields */ }Expand description
A struct that contains necessary data for runtime. Data is stored in
Arc, so it is cheap to clone in your App’s code.
§Example
// simple API creation
let deepl = DeepLApi::with("Your DeepL Key").new();
// **OR** customize it
let duration = std::time::Duration::from_secs(30);
let client = reqwest::Client::builder()
.timeout(duration)
.build()
.unwrap();
// use the pro version API, and a custom client with
// 30 secs timeout
let deepl = DeepLApi::with("Your DeepL Key")
.is_pro(true)
.client(client)
.new();Implementations§
Source§impl DeepLApi
impl DeepLApi
Sourcepub fn upload_document(
&self,
fp: impl Into<PathBuf>,
target_lang: Lang,
) -> UploadDocumentRequester<'_>
pub fn upload_document( &self, fp: impl Into<PathBuf>, target_lang: Lang, ) -> UploadDocumentRequester<'_>
Upload document to DeepL API server, return UploadDocumentResp for
querying the translation status and to download the translated document once
translation is complete.
§Example
use deepl::DeepLApi;
let key = std::env::var("DEEPL_API_KEY").unwrap();
let deepl = DeepLApi::with(&key).new();
// Upload the file to DeepL
let filepath = std::path::PathBuf::from("./hamlet.txt");
let response = deepl.upload_document(&filepath, Lang::ZH)
.source_lang(Lang::EN)
.filename("Hamlet.txt".to_string())
.formality(Formality::Default)
.glossary_id("def3a26b-3e84-45b3-84ae-0c0aaf3525f7".to_string())
.await
.unwrap();Read the example upload_document in repository for detailed usage
Sourcepub async fn check_document_status(
&self,
ident: &UploadDocumentResp,
) -> Result<DocumentStatusResp, Error>
pub async fn check_document_status( &self, ident: &UploadDocumentResp, ) -> Result<DocumentStatusResp, Error>
Check the status of document, returning DocumentStatusResp if success.
Sourcepub async fn download_document<O: AsRef<Path>>(
&self,
ident: &UploadDocumentResp,
output: O,
) -> Result<PathBuf, Error>
pub async fn download_document<O: AsRef<Path>>( &self, ident: &UploadDocumentResp, output: O, ) -> Result<PathBuf, Error>
Download the possibly translated document. Downloaded document will store to the given
output path.
Return downloaded file’s path if success
Source§impl DeepLApi
impl DeepLApi
Sourcepub fn create_glossary(
&self,
name: impl ToString,
) -> CreateGlossaryBuilder<'_, ((&'_ DeepLApi,), (String,), (), (), (), ())>
pub fn create_glossary( &self, name: impl ToString, ) -> CreateGlossaryBuilder<'_, ((&'_ DeepLApi,), (String,), (), (), (), ())>
API for endpoint: https://www.deepl.com/de/docs-api/glossaries/create-glossary. The function for creating a glossary returns a JSON object containing the ID of the newly created glossary and a boolean flag that indicates if the created glossary can already be used in translate requests.
§Example
use crate::{glossary::EntriesFormat, DeepLApi, Lang};
let key = std::env::var("DEEPL_API_KEY").unwrap();
let deepl = DeepLApi::with(&key).new();
let _: CreateGlossaryResp = deepl
.create_glossary("My Glossary")
.source("Hello", Lang::EN)
.target("Guten Tag", Lang::DE)
.format(EntriesFormat::CSV) // This field is optional, we will use TSV as default.
.send()
.await
.unwrap();Sourcepub async fn list_all_glossaries(&self) -> Result<Vec<GlossaryResp>, Error>
pub async fn list_all_glossaries(&self) -> Result<Vec<GlossaryResp>, Error>
List all glossaries and their meta-information, but not the glossary entries.
Sourcepub async fn retrieve_glossary_details(
&self,
id: impl ToString,
) -> Result<GlossaryResp, Error>
pub async fn retrieve_glossary_details( &self, id: impl ToString, ) -> Result<GlossaryResp, Error>
Retrieve meta information for a single glossary, omitting the glossary entries. Require a unique ID assigned to the glossary.
Sourcepub async fn delete_glossary(&self, id: impl ToString) -> Result<(), Error>
pub async fn delete_glossary(&self, id: impl ToString) -> Result<(), Error>
Deletes the specified glossary.
Sourcepub async fn retrieve_glossary_entries(
&self,
id: impl ToString,
) -> Result<Vec<(String, String)>, Error>
pub async fn retrieve_glossary_entries( &self, id: impl ToString, ) -> Result<Vec<(String, String)>, Error>
List the entries of a single glossary in the format specified by the Accept header. Currently, support TSV(tab separated value) only.
Sourcepub async fn list_glossary_language_pairs(
&self,
) -> Result<Vec<GlossaryLanguagePair>, Error>
pub async fn list_glossary_language_pairs( &self, ) -> Result<Vec<GlossaryLanguagePair>, Error>
Retrieve the list of language pairs supported by the glossary feature.
Source§impl DeepLApi
impl DeepLApi
Source§impl DeepLApi
impl DeepLApi
Sourcepub fn translate_text(
&self,
text: impl ToString,
target_lang: Lang,
) -> TranslateRequester<'_>
pub fn translate_text( &self, text: impl ToString, target_lang: Lang, ) -> TranslateRequester<'_>
Translate the given text with specific target language.
§Error
Return Error if the http request fail
§Example
- Simple translation
use deepl::{DeepLApi, Lang};
let key = std::env::var("DEEPL_API_KEY").unwrap();
let deepl = DeepLApi::with(&key).new();
let response = deepl.translate_text("Hello World", Lang::ZH).await.unwrap();
assert!(!response.translations.is_empty());- Translation with XML tag enabled
use deepl::{DeepLApi, Lang};
let key = std::env::var("DEEPL_API_KEY").unwrap();
let deepl = DeepLApi::with(&key).new();
let str = "Hello World <keep>This will stay exactly the way it was</keep>";
let response = deepl
.translate_text(str, Lang::DE)
.source_lang(Lang::EN)
.ignore_tags(vec!["keep".to_owned()])
.tag_handling(TagHandling::Xml)
.await
.unwrap();
let translated_results = response.translations;
let should = "Hallo Welt <keep>This will stay exactly the way it was</keep>";
assert_eq!(translated_results[0].text, should);Source§impl DeepLApi
impl DeepLApi
Sourcepub async fn get_usage(&self) -> Result<UsageResponse, Error>
pub async fn get_usage(&self) -> Result<UsageResponse, Error>
Get the current DeepL API usage
§Example
use deepl::DeepLApi;
let key = std::env::var("DEEPL_API_KEY").unwrap();
let deepl = DeepLApi::with(&key).new();
let response = deepl.get_usage().await.unwrap();
assert_ne!(response.character_count, 0);