[][src]Struct text_translator::Yandex

pub struct Yandex<'a> { /* fields omitted */ }

Yandex Translate API

A struct representing the Yandex Translate API.

This API needs a key, which can be provided at this page.

It implements:

  • language translation, with the default Api trait
  • language detection, with the ApiDetect trait
  • API key, with the ApiKey trait

To use it, first construct the struct with a defined API key, then do the desired function calls.


Important: In order to use those examples, you need to get a free API Key on the Yandex website and replace the YANDEX_API_KEY const with it.

Text translation

Translate a text from an unknown language to Japanese:

use text_translator::*;

// set your personnal API key
const YANDEX_API_KEY: &str = "trnsl.1.1.20200507T202428Z.5e03932d06f63e6a.6ca69498c3b22bff94f6eda9ad8c21b4c3320078";

// construct the struct
let translator: Yandex = Yandex::with_key(YANDEX_API_KEY);

let text: String = "Hello, my name is Naruto Uzumaki!".to_string();

// translate the text, returns a `Result<String, Error>`
let translated_text: String = match translator.translate(text, InputLanguage::Automatic, Language::Japanese) {
    Ok(result) => result,
    Err(err) => panic!("API error, could not translate text : {:#?}", err)

assert_eq!(translated_text, "こんにちは、鳴門のうずまき!")

Language detection

Detect the language of a text:

use text_translator::*;

const YANDEX_API_KEY: &str = "trnsl.1.1.20200507T202428Z.5e03932d06f63e6a.6ca69498c3b22bff94f6eda9ad8c21b4c3320078";

let translator: Yandex = Yandex::with_key(YANDEX_API_KEY);
let text: String = "Bonjour, je m'appelle Naruto Uzumaki!".to_string();

// detect the language, returns a `Result<Option<Language>, Error>`
let detected_language: Language = match translator.detect(text) {
    Ok(response) => match response {
        Some(language) => language,
        None => panic!("Could detect language : unknown language"),
    Err(err) => panic!("API error, could not detect language : {:#?}", err)

assert_eq!(detected_language, Language::French)


impl<'a> Yandex<'a>[src]

pub const fn with_key(key: &'a str) -> Self[src]

Returns a new Yandex struct with the given API key.

Can be used in constant definitions.

Trait Implementations

impl<'a> Api for Yandex<'a>[src]

fn new() -> Self[src]

Returns a new Yandex struct without API key.

To set it, use with_key or set_key methods instead.

impl<'a> ApiDetect for Yandex<'a>[src]

impl<'a> ApiKey<'a> for Yandex<'a>[src]

impl<'a> Clone for Yandex<'a>[src]

impl<'a> Copy for Yandex<'a>[src]

impl<'a> Debug for Yandex<'a>[src]

impl<'a> Eq for Yandex<'a>[src]

impl<'a> Ord for Yandex<'a>[src]

impl<'a> PartialEq<Yandex<'a>> for Yandex<'a>[src]

impl<'a> PartialOrd<Yandex<'a>> for Yandex<'a>[src]

impl<'a> StructuralEq for Yandex<'a>[src]

impl<'a> StructuralPartialEq for Yandex<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Yandex<'a>

impl<'a> Send for Yandex<'a>

impl<'a> Sync for Yandex<'a>

impl<'a> Unpin for Yandex<'a>

impl<'a> UnwindSafe for Yandex<'a>

Blanket Implementations

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

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

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

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized

impl<T> From<T> for T[src]

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

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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.