Skip to content

Commit

Permalink
Release v0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
rikonaka committed Jul 7, 2023
1 parent 852cab7 commit 3136c8f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ Click on a word or a paragraph (tested on Ubuntu 22.04 Gnome with Wayland and X1

## Option Description

### Change API provider

The translation API for DeepL is now supported, but requires the user to have an auth key.

```bash
translator-rs -a deepl --auth-key xxxxxxxx:xx
```

or pro

```bash
translator-rs -a deeplpro --auth-key xxxxxxxx:xx
```

### Proxy Options

**The Google Translate API has been blacklisted in China (2022-9-29), according to the latest firewall rules (GFW), so a proxy option has been added.**
Expand Down
56 changes: 50 additions & 6 deletions src/deepl_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ pub struct DeepLFreeResponse {
pub translations: Vec<Translation>,
}

pub async fn translate_free(
async fn tranlate(
sl: &str, // source language
tl: &str, // target language
translate_string: &str,
proxy_str: &str,
auth_key: &str,
translate_url: &str,
) -> Result<Vec<Item>> {
if auth_key == "null" {
panic!("Please set auth_key!");
}

let translate_url = format!("https://api-free.deepl.com/v2/translate");
let translate_string = fliter_long(translate_string);
let translate_string = fliter_short(&translate_string);

Expand Down Expand Up @@ -71,6 +67,54 @@ pub async fn translate_free(
Ok(result_vec)
}

pub async fn translate_free(
sl: &str, // source language
tl: &str, // target language
translate_string: &str,
proxy_str: &str,
auth_key: &str,
) -> Result<Vec<Item>> {
if auth_key == "null" {
panic!("Please set auth_key!");
}
let translate_url = format!("https://api-free.deepl.com/v2/translate");
tranlate(
sl,
tl,
translate_string,
proxy_str,
auth_key,
&translate_url,
)
.await
}

pub async fn translate_pro(
sl: &str, // source language
tl: &str, // target language
translate_string: &str,
proxy_str: &str,
auth_key: &str,
) -> Result<Vec<Item>> {
if auth_key == "null" {
panic!("Please set auth_key!");
}

if auth_key == "null" {
panic!("Please set auth_key!");
}
let translate_url = format!("https://api.deepl.com/v2/translate");
tranlate(
sl,
tl,
translate_string,
proxy_str,
auth_key,
&translate_url,
)
.await
}

// #[cfg(test)]
// mod tests {
// use super::*;
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod google_api;
mod utils;
// mod youdao_api;

use deepl_api::translate_free;
use deepl_api::{translate_free, translate_pro};
use google_api::{translate_longstring, translate_shortword};
use utils::{convert_language, get_text};

Expand Down Expand Up @@ -89,7 +89,13 @@ async fn translate<'a>(
vec![]
}
},
"youdao" => Vec::new(),
"deeplpro" => match translate_pro(sl, tl, translate_string, proxy_str, auth_key).await {
Ok(t) => t,
Err(e) => {
println!("translate failed: {}", e);
vec![]
}
},
_ => panic!("Unsupported API provider"),
};
// println!("{:?}", result_vec);
Expand Down Expand Up @@ -305,7 +311,7 @@ async fn main() -> Result<()> {
}
}
} else if cfg!(target_os = "windows") {
println!("Working...");
println!("{}{}{}", "Working with ", api, "...");
let mut sub_clear = clear_times;
loop {
thread::sleep(sleep_time);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn convert_language<'a>(
convert_language
}
_ => panic!("Unsupport api provider!"),
};
};
let sl_result = convert_language(source_language);
let tl_result = convert_language(target_language);
(sl_result, tl_result)
Expand Down

0 comments on commit 3136c8f

Please sign in to comment.