Crates.io | text-translator |
lib.rs | text-translator |
version | 0.1.0 |
source | src |
created_at | 2020-05-10 15:14:23.951998 |
updated_at | 2020-05-10 15:14:23.951998 |
description | A library to translate text between languages, using selected engine API |
homepage | |
repository | https://github.com/aunetx/text-translator |
max_upload_size | |
id | 239718 |
size | 38,288 |
This crate permits to translate text between languages easily. Its goals are:
It wants to implement the following APIs:
[x]
Yandex.Translate
[x]
with API key[ ]
without key (5_000 chars/translation max)[ ]
Google Translate[ ]
BingTo 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 detectionLanguages are represented with the Language
enum for target language, and InputLanguage
for input language.
See their respective documentations for more.
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.
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, "こんにちは、鳴門のうずまき!")
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)
All contributions are welcome!
If you find any issue, please report it — if you have recommendations too!