Skip to main content

多引擎命令行翻译工具,支持有道、OpenAI 兼容接口和 DeepL 三种翻译后端。

Project description

♿电棍翻译器

多引擎命令行翻译工具,支持有道、OpenAI 兼容接口和 DeepL 三种翻译后端。


特性

  • 多引擎:有道 API、OpenAI / DeepSeek / 任何兼容接口、DeepL,一键切换
  • 多模式:命令行参数、逐行交互、管道/heredoc,灵活适配各种使用场景
  • 缓存:SQLite 持久化翻译缓存,相同文本不重复请求 API
  • 智能语言代码:ISO 639 语言代码,支持大小写混写、BCP47 扩展(zh-Hans 等)
  • 异步:基于 asyncio + httpx,批量翻译时并发请求,无需等待

快速开始

pip install otto-trans

# 首次运行自动生成配置文件
otto -t zh-Hans Hello, world!

# 使用有道翻译
otto -e youdao -o app_key=xxx -o app_secret=yyy -t zh-Hans hello

# 使用 DeepSeek
otto -e openai:deepseek -t zh-Hans hello

# 使用 DeepL
otto -e deepl -o auth_key=xxx -t zh-Hans hello

使用方式

命令行参数(单段)

otto -t zh-Hans Hello world
# → 合并为 "Hello world" 翻译成中⽂

逐行模式(多段独立翻译)

otto -t zh-Hans -b
请输入要翻译的文本(输入空行结束):
hello
world
[回车]

管道模式

echo "hello\nworld" | otto -t zh-Hans

批量模式

otto -t zh-Hans -b hello world foo
# → hello、world、foo 分别翻译

选项

-f, --from       源语言(默认 auto)
-t, --to         目标语言
-e, --engine     翻译引擎(youdao / openai / openai:provider / deepl)
-o, --option     引擎选项,格式 key=value
-b, --batch      批量模式,每段文本独立翻译
    --reset-config  重置配置文件
    --reset-cache   重置翻译缓存

引擎选项

引擎 必需参数 可选参数
有道 app_keyapp_secret
OpenAI 兼容 endpointapi_keymodel prompt_templatethinkingreasoning_efforttemperaturemax_tokenstop_p
DeepL auth_key paidcontextpreserve_formattingformalitymodel_type

配置

配置文件位于 ~/.config/otto-trans/config.yaml,首次运行自动生成。

default_engine: youdao
default_from: auto
default_to: ""

engines:
  youdao:
    app_key: ""
    app_secret: ""

  # openai:
  #   your-provider:
  #     endpoint: ""
  #     api_key: ""
  #     model: ""
  #     ...

通过 -o key=value 可在命令行临时覆盖配置文件中的参数。


架构

otto-trans/
├── cli.py           Typer CLI 入口
├── config/
│   └── settings.py  配置管理(Pydantic + YAML)
├── core/
│   ├── cache.py      SQLite 翻译缓存(单例模式)
│   └── translator.py 翻译编排器(Facade 模式)
└── engine/
    ├── base.py       策略模式抽象基类
    ├── youdao.py     有道翻译引擎
    ├── openai.py     OpenAI 兼容接口引擎
    └── deepl.py      DeepL 翻译引擎

设计模式

模式 应用
策略模式 BaseTranslator 定义接口,三个引擎各自实现
模板方法 translate_batch 基类提供默认并发,子类可覆盖
外观模式 Translator 隐藏缓存和引擎的协调逻辑
单例模式 Cache 全局唯一 SQLite 连接
工厂方法 UnsupportedLanguageError.for_engine() 统一错误格式
依赖注入 Translator 接收 engine 和 cache 作为依赖

开发

# 克隆并安装
git clone https://github.com/xuangeyouneihan/otto-trans
cd otto-trans
uv sync

# 运行测试
pytest

# 运行
uv run otto -t zh-Hans hello

测试

24 个测试覆盖:缓存 CRUD、引擎初始化校验、语言代码归一化、签名构造、HTTP 请求验证、翻译编排逻辑。

pytest          # 全部测试
pytest -v       # 详细输出
pytest -k cache # 只跑缓存相关测试

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

otto_trans-0.1.5.tar.gz (75.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

otto_trans-0.1.5-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file otto_trans-0.1.5.tar.gz.

File metadata

  • Download URL: otto_trans-0.1.5.tar.gz
  • Upload date:
  • Size: 75.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for otto_trans-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0d4e17c793a2550c468c6c821de07ba6b15e0cf3cee59ca39166b685c8f6a753
MD5 40f4d46ecccae6229ff9be15deb176c0
BLAKE2b-256 36952458067cfdc60a493bca95e3242eea2eb576d7ef99e735033846055ba990

See more details on using hashes here.

File details

Details for the file otto_trans-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: otto_trans-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for otto_trans-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ecece8f95b5274bff3cf0efc769f75dfeb0a5e3c2b3af6d31a8eb4b492b2358e
MD5 81a16e76736fde90554b20f39e8436ec
BLAKE2b-256 9a212a14360516021a5871b457e25ae89fd8381116dbe39c1383ff8168cc6f51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page