Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tools/baidu-translate/1.0.0/百度翻译.tool
Binary file not shown.
120 changes: 120 additions & 0 deletions tools/baidu-translate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# 百度翻译 API 工具

基于百度翻译 API 封装的 Python 翻译工具函数,支持多语言互译,提供自动语言检测、MD5 签名认证、错误码详细说明等功能,简化翻译接口调用流程。

## 一、项目介绍

### 1.1 核心功能

- **多语言支持**:支持中文、英语、日语、韩语、法语、西班牙语、俄语、德语等多种语言互译,覆盖主流应用场景。
- **自动语言检测**:源语言默认 `auto` 自动检测,无需手动指定原文语种,提升易用性。
- **安全签名机制**:采用 MD5(appid + 原文 + salt + 密钥) 标准签名算法,确保请求合法性与数据安全。
- **智能错误处理**:对 API 错误码进行分类解析,提供详细的错误原因与解决建议(如签名错误、QPS 超限、余额不足等),便于快速定位问题。
- **结构化响应**:成功返回翻译后的目标语言文本,失败返回格式化的错误信息,统一异常处理逻辑。

### 1.2 适用场景

- **多语言内容处理**:网站国际化、多语言文档翻译、客服自动回复等场景。
- **爬虫数据翻译**:抓取外语网站内容后自动翻译为目标语言,便于数据分析。
- **接口集成开发**:在应用中集成翻译功能,如聊天机器人、翻译工具等。
- **自动化测试**:验证多语言功能的正确性,批量翻译测试数据。

---

## 二、环境准备

### 2.1 依赖库

该工具基于 Python 标准库构建,无需额外安装第三方依赖(`requests` 除外)。

| 依赖库 | 版本要求 | 用途说明 |
| ---------- | ---------------- | ---------------------------- |
| `requests` | ≥ 2.20.0 | 发送 HTTP POST 请求 |
| `hashlib` | Python 标准库 | 生成 MD5 签名 |
| `random` | Python 标准库 | 生成随机数 salt,防重放攻击 |

### 2.2 安装依赖

通过 `pip` 安装 `requests` 库:

```bash
pip install requests
```

---

## 三、使用说明

### 3.1 API 密钥获取

使用前需在 [百度翻译开放平台](https://fanyi-api.baidu.com/) 注册账号并创建应用,获取以下凭证:

- **APP ID**:应用的唯一标识符
- **密钥(Secret Key)**:用于生成签名的密钥

> **注意**:标准版免费额度为每月 5 万字符,QPS 限制为 1 次/秒。超额需升级付费版或联系客服增加配额。

---

### 3.2 函数定义

```python
def baidu_translate(text, to_lang, app_id, secret_key, from_lang):
"""
调用百度翻译API进行文本翻译

参数:
text (str): 需要翻译的原文
to_lang (str): 目标语言,如 'en'(英语)、'zh'(中文)、'jp'(日语)等
app_id (str): 百度翻译API的APP ID
secret_key (str): 百度翻译API的密钥
from_lang (str): 源语言,默认'auto'自动检测

返回:
str: 翻译后的文本,失败返回错误信息

常用语言代码:
zh: 中文
en: 英语
jp: 日语
kor: 韩语
fra: 法语
spa: 西班牙语
ru: 俄语
de: 德语
"""
```

---

### 3.3 参数说明

#### 必需参数

| 参数名 | 类型 | 说明 | 示例 |
| :----------- | :--- | :----------------------------- | :------------------------- |
| `text` | str | 需要翻译的原文内容 | `'Hello World'` |
| `to_lang` | str | 目标语言代码 | `'zh'`(翻译为中文) |
| `app_id` | str | 百度翻译 API 的 APP ID | `'2015063000000001'` |
| `secret_key` | str | 百度翻译 API 的密钥(Secret Key) | `'12345678'` |

#### 可选参数

| 参数名 | 类型 | 默认值 | 说明 |
| :---------- | :--- | :----- | :--------------------------------------------- |
| `from_lang` | str | `'auto'` | 源语言代码,默认自动检测;可指定如 `'en'`、`'zh'` |

#### 常用语言代码对照表

| 语言 | 代码 | 语言 | 代码 |
| :--------- | :------ | :--------- | :---- |
| 中文 | `zh` | 英语 | `en` |
| 日语 | `jp` | 韩语 | `kor` |
| 法语 | `fra` | 西班牙语 | `spa` |
| 俄语 | `ru` | 德语 | `de` |
| 阿拉伯语 | `ara` | 泰语 | `th` |
| 越南语 | `vie` | 意大利语 | `it` |

> 完整语言代码列表请参考 [百度翻译 API 文档](https://fanyi-api.baidu.com/doc/21)。

---
5 changes: 5 additions & 0 deletions tools/baidu-translate/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 百度翻译
tags:
- 翻译工具
title: 百度翻译
description: 基于百度翻译 API 封装的 Python 翻译工具函数,支持多语言互译,提供自动语言检测、MD5 签名认证、错误码详细说明等功能,简化翻译接口调用流程。
Binary file added tools/baidu-translate/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.