pub struct DeepL { /* private fields */ }
Expand description
The DeepL
client struct
Implementations§
source§impl DeepL
impl DeepL
sourcepub fn document_upload(&self, opt: DocumentOptions) -> Result<Document, Error>
pub fn document_upload(&self, opt: DocumentOptions) -> Result<Document, Error>
POST /document
Upload a document.
Translating a document consists of three steps: upload, polling translation status,
and download. document_upload
returns a document handle that we need in order to
complete the remaining steps: getting the document_status
and finally fetching the translation result with
document_download
.
§Example
let file_path = PathBuf::from("test.txt");
let target_lang = Language::DE;
let opt = DocumentOptions::new(target_lang, file_path);
let doc = dl.document_upload(opt).unwrap();
while !dl.document_status(&doc).unwrap().is_done() {
// wait a second
thread::sleep(Duration::from_secs(1));
}
let out_file = PathBuf::from("test-translated.txt");
let _ = dl.document_download(doc, Some(out_file.clone())).unwrap();
let content = fs::read_to_string(out_file).unwrap();
assert!(!content.is_empty());
sourcepub fn document_status(&self, doc: &Document) -> Result<DocumentStatus, Error>
pub fn document_status(&self, doc: &Document) -> Result<DocumentStatus, Error>
POST /document/{document_id}
Get document translation status. In case there’s an issue with translation,
DocumentStatus
contains a field error_message
that may provide context
for the cause of the error.
source§impl DeepL
impl DeepL
sourcepub fn glossary_languages(&self) -> Result<GlossaryLanguagePairsResult, Error>
pub fn glossary_languages(&self) -> Result<GlossaryLanguagePairsResult, Error>
GET /glossary-language-pairs
Get supported glossary language pairs
sourcepub fn glossary_new(
&self,
name: String,
source_lang: Language,
target_lang: Language,
entries: String,
fmt: GlossaryEntriesFormat
) -> Result<Glossary, Error>
pub fn glossary_new( &self, name: String, source_lang: Language, target_lang: Language, entries: String, fmt: GlossaryEntriesFormat ) -> Result<Glossary, Error>
POST /glossaries
Create a new glossary.
DeepL
supports creating custom glossaries, i.e. a collection of entries which when
used in a translation ensures that a given word from the source language always maps to
the same target word in the glossary, giving a user more control in cases where
translation might otherwise be unreliable or ambiguous. A given glossary is defined by one
source language and one target language where the source word in each entry is unique.
§Example
let name = "my_glossary".to_string();
let source_lang = Language::EN;
let target_lang = Language::IT;
let entries = "hello,ciao".to_string();
let fmt = GlossaryEntriesFormat::Csv;
let glossary = dl.glossary_new(
name,
source_lang,
target_lang,
entries,
fmt
)
.unwrap();
assert!(!glossary.glossary_id.is_empty());
sourcepub fn glossaries(&self) -> Result<GlossariesResult, Error>
pub fn glossaries(&self) -> Result<GlossariesResult, Error>
GET /glossaries
List current active glossaries
sourcepub fn glossary_info(&self, glossary_id: &str) -> Result<Glossary, Error>
pub fn glossary_info(&self, glossary_id: &str) -> Result<Glossary, Error>
GET /glossaries/{glossary_id}
Get meta information for a specified glossary (excluding entries)
source§impl DeepL
impl DeepL
sourcepub fn languages(
&self,
lang_type: LanguageType
) -> Result<Vec<LanguageInfo>, Error>
pub fn languages( &self, lang_type: LanguageType ) -> Result<Vec<LanguageInfo>, Error>
GET /languages
Get information on supported languages.
§Example
let source_langs = dl.languages(LanguageType::Source).unwrap();
assert!(!source_langs.is_empty());
let language = &source_langs[0];
println!("{}", language.language); // BG
println!("{}", language.name); // Bulgarian
source§impl DeepL
impl DeepL
sourcepub fn translate(
&self,
opt: TextOptions,
text: Vec<String>
) -> Result<TranslateTextResult, Error>
pub fn translate( &self, opt: TextOptions, text: Vec<String> ) -> Result<TranslateTextResult, Error>
POST /translate
Translate one or more text strings.
To translate text all we need is to specify a target language and a chunk of text to translate.
In addition, the TextOptions
type exposes a number of methods used to control formatting,
set a desired formality, or tell the server how to handle HTML or XML tags.
§Example
Translate text.
let text = vec!["good morning"];
let res = dl.translate(
TextOptions::new(Language::ES),
vec!["good morning".to_string()],
)
.unwrap();
assert!(!res.translations.is_empty());
Translate text inside HTML. Note we can skip translation for tags with with the special “notranslate” attribute.
let raw_html = r#"
<h2 class="notranslate">Good morning.</h2>
<p>To be or not to be, that is the question.</p>"#;
let text = vec![raw_html.to_string()];
let opt = TextOptions::new(Language::ES)
.tag_handling(TagHandling::Html)
.outline_detection(false);
let res = dl.translate(opt, text).unwrap();
assert!(!res.translations.is_empty());
§Errors
If target language and (optionally provided) source language are an invalid pair.