pub struct Yandex<'a> { /* private fields */ }
Expand description
§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.
§Examples
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)
Implementations§
Trait Implementations§
Source§impl<'a> Ord for Yandex<'a>
impl<'a> Ord for Yandex<'a>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'a> PartialOrd for Yandex<'a>
impl<'a> PartialOrd for Yandex<'a>
impl<'a> Copy for Yandex<'a>
impl<'a> Eq for Yandex<'a>
impl<'a> StructuralPartialEq for Yandex<'a>
Auto Trait Implementations§
impl<'a> Freeze for Yandex<'a>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.