1 unstable release
0.1.0 | May 10, 2020 |
---|
#6 in #translate-text
27KB
633 lines
text_translator
Description
This crate permits to translate text between languages easily. Its goals are:
- implementing an unique library for different APIs
- permitting language translations / detections with or withtout API key when possible
- ease of use / relative performances
- (later) async translations
It wants to implement the following APIs:
[x]
Yandex.Translate[x]
with API key[ ]
without key (5_000 chars/translation max)
[ ]
Google Translate[ ]
Bing
How to use
To use it, you first need to construct a translator (a struct implementing the Api trait).
Then, you will be able to do various function calls on this struct:
my_translator.translate(my_text, input_language, target_language)
my_translator.detect(my_text)
if the API implements language detection
Languages are represented with the Language
enum for target language, and InputLanguage
for input language.
See their respective documentations for more.
Examples
For the moment, only the Yandex API is implemented.
Those are examples on how to use it to translate a text, and to detect the input language.
Important: In order to use those examples, you need to get a free API Key on the Yandex website.
Text translation
Translate a text from an unknown language to Japanese:
use text_translator::*;
// replace with your personnal API key
const YANDEX_API_KEY: &str = "MY_PRIVATE_KEY_SET_YOUR_OWN";
// 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::*;
// replace with your personnal API key
const YANDEX_API_KEY: &str = "MY_PRIVATE_KEY_SET_YOUR_OWN";
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)
Contributing
All contributions are welcome!
If you find any issue, please report it — if you have recommendations too!
Dependencies
~9–19MB
~266K SLoC