Struct deeprl::DeepL

source ·
pub struct DeepL { /* private fields */ }
Expand description

The DeepL client struct

Implementations§

source§

impl DeepL

source

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());
source

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

pub fn document_download( &self, doc: Document, out_file: Option<PathBuf> ) -> Result<PathBuf, Error>

POST /document/{document_id}/result

Download translated document.

If no out_file is given, the returned file path will have the name of the Document id.

source§

impl DeepL

source

pub fn glossary_languages(&self) -> Result<GlossaryLanguagePairsResult, Error>

GET /glossary-language-pairs

Get supported glossary language pairs

source

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());
source

pub fn glossaries(&self) -> Result<GlossariesResult, Error>

GET /glossaries

List current active glossaries

source

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

pub fn glossary_entries( &self, glossary_id: &str ) -> Result<HashMap<String, String>, Error>

GET /glossaries/{glossary_id}/entries

Retrieve entries for a specified glossary.

source

pub fn glossary_delete(&self, glossary_id: &str) -> Result<(), Error>

DELETE /glossaries/{glossary_id}

Destroy a glossary

source§

impl DeepL

source

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

source

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.

source§

impl DeepL

source

pub fn new(key: &str) -> Self

Create a new instance of DeepL from an API key.

source

pub fn client(&mut self, client: Client) -> &mut Self

Sets a user-defined HTTP client

source

pub fn set_app_info(&mut self, app: String) -> &mut Self

Sets app name and version to be used in the User-Agent header, e.g. “my-app/1.2.3”

source

pub fn usage(&self) -> Result<Usage, Error>

GET /usage

Get account usage

Auto Trait Implementations§

§

impl Freeze for DeepL

§

impl !RefUnwindSafe for DeepL

§

impl Send for DeepL

§

impl Sync for DeepL

§

impl Unpin for DeepL

§

impl !UnwindSafe for DeepL

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more