Skip to content

Commit 7ac2b16

Browse files
committed
added deeplx support
1 parent 849ee1d commit 7ac2b16

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ While searching for similar projects, I found Mozilla's [translation-service](ht
2424
- [Immersive Translate](https://immersivetranslate.com/) API
2525
- [Kiss Translator](https://www.kis-translator.com/) API
2626
- [HCFY](https://hcfy.app/) API
27+
- [DeepLX](https://github.com/OwO-Network/DeepLX) API
2728
- 🔑 API key protection support
2829
- 🐳 Docker deployment ready
2930

@@ -272,6 +273,33 @@ Response:
272273
}
273274
```
274275

276+
#### DeepLX API
277+
278+
```
279+
POST /translate
280+
```
281+
282+
Request body:
283+
```json
284+
{
285+
"text": "Hello world",
286+
"source_lang": "EN",
287+
"target_lang": "ZH"
288+
}
289+
```
290+
291+
Response:
292+
```json
293+
{
294+
"code": 200,
295+
"id": 1744646400,
296+
"data": "你好世界",
297+
"alternatives": [],
298+
"source_lang": "EN",
299+
"target_lang": "ZH"
300+
}
301+
```
302+
275303
### Health Check
276304

277305
```

README_ZH.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [沉浸式翻译 (Immersive Translate)](https://immersivetranslate.com/) API
2525
- [简约翻译 (Kiss Translator)](https://www.kis-translator.com/) API
2626
- [划词翻译 (HCFY)](https://hcfy.app/) API
27+
- [DeepLX](https://github.com/OwO-Network/DeepLX) API
2728
- 🔑 支持 API 密钥保护
2829
- 🐳 提供 Docker 镜像,便于部署
2930

@@ -270,6 +271,30 @@ POST /hcfy
270271
}
271272
```
272273

274+
#### DeepLX API
275+
276+
```
277+
POST /translate
278+
```
279+
280+
请求体:
281+
```json
282+
{
283+
"text": "Hello world",
284+
"source_lang": "EN", // 可选,省略则自动检测
285+
"target_lang": "ZH"
286+
}
287+
```
288+
289+
响应:
290+
```json
291+
{
292+
"code": 200,
293+
"data": "你好世界",
294+
"alternatives": []
295+
}
296+
```
297+
273298
### 健康检查
274299

275300
```

server/src/endpoint.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
};
55
use axum::{Json, extract::State};
66
use serde::{Deserialize, Serialize};
7-
use std::sync::Arc;
7+
use std::{sync::Arc, time::SystemTime};
88

99
#[derive(Debug, Deserialize)]
1010
pub struct DetectLanguageRequest {
@@ -144,9 +144,6 @@ pub async fn translate_hcfy(
144144
const LANGUAGE_CODE_MAP: &[(&str, &str)] =
145145
&[("中文(简体)", "zh"), ("英语", "en"), ("日语", "jp")];
146146

147-
const LANGUAGE_NAME_MAP: &[(&str, &str)] =
148-
&[("zh", "中文(简体)"), ("en", "英语"), ("jp", "日语")];
149-
150147
fn convert_language_name(lang: &str) -> String {
151148
LANGUAGE_CODE_MAP
152149
.iter()
@@ -157,10 +154,10 @@ pub async fn translate_hcfy(
157154
}
158155

159156
fn get_language_name(code: &str) -> String {
160-
LANGUAGE_NAME_MAP
157+
LANGUAGE_CODE_MAP
161158
.iter()
162-
.find(|&&(c, _)| c == code)
163-
.map(|&(_, name)| name)
159+
.find(|&&(_, c)| c == code)
160+
.map(|&(name, _)| name)
164161
.unwrap_or(code)
165162
.to_string()
166163
}
@@ -189,3 +186,47 @@ pub async fn translate_hcfy(
189186
result: vec![translated_text],
190187
}))
191188
}
189+
190+
#[derive(Debug, Deserialize)]
191+
pub struct DeeplxTranslationRequest {
192+
text: String,
193+
source_lang: String,
194+
target_lang: String,
195+
}
196+
197+
#[derive(Debug, Serialize)]
198+
pub struct DeeplxTranslationResponse {
199+
code: u32,
200+
id: u128,
201+
data: String,
202+
alternatives: Vec<String>,
203+
source_lang: String,
204+
target_lang: String,
205+
method: String,
206+
}
207+
208+
pub async fn translate_deeplx(
209+
State(state): State<Arc<AppState>>,
210+
Json(request): Json<DeeplxTranslationRequest>,
211+
) -> Result<Json<DeeplxTranslationResponse>, AppError> {
212+
let (text, from_lang, to_lang) = perform_translation(
213+
&state,
214+
&request.text,
215+
Some(request.source_lang.to_lowercase()),
216+
&request.target_lang.to_lowercase(),
217+
)
218+
.await?;
219+
220+
Ok(Json(DeeplxTranslationResponse {
221+
code: 200,
222+
id: SystemTime::now()
223+
.duration_since(SystemTime::UNIX_EPOCH)
224+
.unwrap()
225+
.as_millis(),
226+
data: text,
227+
alternatives: vec![],
228+
source_lang: from_lang.to_uppercase(),
229+
target_lang: to_lang.to_uppercase(),
230+
method: "Free".to_owned(),
231+
}))
232+
}

server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ async fn main() -> anyhow::Result<()> {
274274
.route("/kiss", post(endpoint::translate_kiss))
275275
.route("/imme", post(endpoint::translate_immersive))
276276
.route("/hcfy", post(endpoint::translate_hcfy))
277+
.route("/deeplx", post(endpoint::translate_deeplx))
277278
.route("/detect", post(endpoint::detect_language))
278279
.route(
279280
"/health",

0 commit comments

Comments
 (0)