NLTranslator Proxy
macOS 翻译代理服务,使用 macOS 快捷指令调用系统翻译功能,提供 HTTP API。
功能特性
- 🍎 使用 macOS 原生系统翻译
- 🚀 简单的 HTTP API
- 🔒 本地运行,数据不离开设备
- 📦 无需外部依赖或 API 密钥
系统要求
- macOS 15.0+
- Swift 5.9+
- 需要创建翻译快捷指令(见下方设置说明)
设置步骤
1. 创建翻译快捷指令
- 打开"快捷指令"应用
- 点击右上角的 "+" 创建新快捷指令
- 添加以下操作:
- 搜索并添加"翻译文本"操作
- 设置为"自动检测"源语言
- 设置目标语言为"中文(简体)"或你需要的语言
- 将快捷指令命名为"翻译文本"
- 保存
2. 编译并运行
服务器将在 http://localhost:8765 启动
API 文档
POST /translate
翻译文本
请求体:
{
"text": "Hello, world!",
"sourceLanguage": "en",
"targetLanguage": "zh-Hans"
}响应:
{
"translatedText": "你好,世界!",
"sourceLanguage": "en",
"targetLanguage": "zh-Hans"
}GET /health
健康检查
响应:
{
"status": "ok",
"service": "NLTranslator Proxy",
"version": "1.0.0"
}GET /models
列出支持的语言对
响应:
{
"models": [
{
"name": "nltranslator:en-zh-Hans",
"sourceLanguage": "en",
"targetLanguage": "zh-Hans"
}
]
}支持的语言
- 英语 (en) ↔ 简体中文 (zh-Hans)
- 英语 (en) ↔ 繁体中文 (zh-Hant)
- 英语 (en) ↔ 日语 (ja)
- 英语 (en) ↔ 韩语 (ko)
- 英语 (en) ↔ 西班牙语 (es)
- 英语 (en) ↔ 法语 (fr)
- 英语 (en) ↔ 德语 (de)
使用示例
cURL
# 翻译文本 curl -X POST http://localhost:8765/translate \ -H "Content-Type: application/json" \ -d '{ "text": "Hello, world!", "sourceLanguage": "en", "targetLanguage": "zh-Hans" }' # 健康检查 curl http://localhost:8765/health # 列出模型 curl http://localhost:8765/models
Python
import requests response = requests.post('http://localhost:8765/translate', json={ 'text': 'Hello, world!', 'sourceLanguage': 'en', 'targetLanguage': 'zh-Hans' }) print(response.json()['translatedText'])
JavaScript
const response = await fetch('http://localhost:8765/translate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text: 'Hello, world!', sourceLanguage: 'en', targetLanguage: 'zh-Hans' }) }); const data = await response.json(); console.log(data.translatedText);
注意事项
- macOS 14+ 提供完整的翻译功能
- macOS 13 可能需要额外配置或功能受限
- 首次使用某个语言对时,系统可能需要下载语言模型
- 翻译质量取决于 macOS 系统的翻译引擎
开发
# 开发模式运行 swift run # 构建 release 版本 swift build -c release # 运行测试 swift test
许可证
MIT License