Skip to content

Commit

Permalink
0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
grantshandy committed May 24, 2021
1 parent c427169 commit efef582
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libretranslate"
version = "0.4.0"
version = "0.4.1"
authors = ["Grant Handy <[email protected]>", "Rafael G. Dantas <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -14,6 +14,7 @@ keywords = ["translate", "language", "libretranslate", "translation"]
[dependencies]
serde_json = "1.0.64"
surf = "2.2.0"
unic-langid = "0.9.0"

[dev-dependencies]
tokio = { version = "1.5.0", features = ["full"]}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A LibreTranslate API client for Rust.
```
libretranslate = "0.4.0"
libretranslate = "0.4.1"
```

`libretranslate` allows you to use open source machine translation in your projects through an easy to use API that connects to the official [webpage](https://libretranslate.com/).
Expand Down
18 changes: 18 additions & 0 deletions examples/unic_langid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use libretranslate::{translate_url, Language};
use unic_langid::LanguageIdentifier;

#[tokio::main]
async fn main() {
let source = Language::English;

let li: LanguageIdentifier = "fr-FR".parse().expect("Failed to parse.");
let target: Language = Language::from_unic_langid(li).unwrap();

let input = "Amazing!";

let data = translate_url(source, target, input, "https://libretranslate.de/").await.unwrap();

println!("URL: \"{}\"", data.url);
println!("Input {}: \"{}\"", data.source.as_pretty(), data.input);
println!("Output {}: \"{}\"", data.target.as_pretty(), data.output);
}
18 changes: 18 additions & 0 deletions src/languages.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;
use crate::error::LanguageError;
use unic_langid::LanguageIdentifier;

/// Languages that can used for input and output of the [`translate`](crate::translate) function.
#[derive(Debug, Clone, PartialEq, Copy, Hash)]
Expand Down Expand Up @@ -56,6 +57,23 @@ impl Language {
pub fn from<T: AsRef<str>>(s: T) -> Result<Self, LanguageError> {
return Self::from_str(s.as_ref());
}

/// Create a Language from a unic_langid::LanguageIdentifier.
pub fn from_unic_langid(s: LanguageIdentifier) -> Result<Self, LanguageError> {
match s.language.as_str() {
"en" => Ok(Language::English),
"ar" => Ok(Language::Arabic),
"zh" => Ok(Language::Chinese),
"fr" => Ok(Language::French),
"de" => Ok(Language::German),
"it" => Ok(Language::Italian),
"pt" => Ok(Language::Portuguese),
"ru" => Ok(Language::Russian),
"es" => Ok(Language::Spanish),
"ja" => Ok(Language::Japanese),
&_ => Err(LanguageError::FormatError("Unknown Language".to_string())),
}
}
}

// TODO: Get locale from user to set Language::default().
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! A LibreTranslate API client for Rust.
//! ```
//! libretranslate = "0.4.0"
//! libretranslate = "0.4.1"
//! ```
//!
//! `libretranslate` allows you to use open source machine translation in your projects through an easy to use API that connects to the official [webpage](https://libretranslate.com/).
Expand Down

0 comments on commit efef582

Please sign in to comment.