text-translator

Crates.iotext-translator
lib.rstext-translator
version0.1.0
sourcesrc
created_at2020-05-10 15:14:23.951998
updated_at2020-05-10 15:14:23.951998
descriptionA library to translate text between languages, using selected engine API
homepage
repositoryhttps://github.com/aunetx/text-translator
max_upload_size
id239718
size38,288
Aurélien Hamy (aunetx)

documentation

README

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:

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:

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!

Commit count: 35

cargo fmt