← 返回
未分类 Key 中文

Baidu Text Translate

Translate text using Baidu AI Translation API via trans-cli with Baidu-specific language codes, API key management, error handling, and environment diagnostics.
通过 trans-cli 使用百度AI翻译API,实现文本翻译,支持百度专用的语言代码、API密钥管理、错误处理及环境诊断。
baidu-translate baidu-translate 来源
未分类 clawhub v1.0.1 1 版本 99864.7 Key: 需要
★ 4
Stars
📥 658
下载
💾 0
安装
1
版本
#latest

概述

baidu_text_translate — Agent Reference

trans text wraps the Baidu AI Translation API. Use --json — it separates

success (stdout) from errors (stderr) and gives you structured fields to act on.

trans text --json "你好世界"                             # auto-detect → English
trans text --json --from zh --to jp "你好"               # explicit languages
trans text --json --to fra --reference "使用正式语气" "你好"  # custom instruction
echo "早上好" | trans text --json                        # stdin pipe

Environment Diagnosis

Before translating, verify the environment with trans doctor:

trans doctor           # human-readable output
trans doctor --json    # JSON output for Agent parsing

JSON contract (stdout):

{
  "checks": [
    {"name":"api_key",     "ok":true,  "source":"env/config", "message":"configured"},
    {"name":"connectivity","ok":true,  "latency_ms":243,       "message":"reachable"},
    {"name":"account",     "ok":true,                          "message":"valid"}
  ],
  "all_ok": true
}
Exitall_okMeaning
-------------------------
0trueAll checks passed — ready to translate
2falseAt least one check failed — inspect checks[].ok

When a check fails, help_url in the failing check points to the fix:

  • api_key.ok:false → set TRANS_API_KEY (see Configuration below)
  • account.ok:false → key invalid/expired or quota exhausted

Run trans doctor --json as the first step when diagnosing any trans-cli failure.


JSON Contract

Success (stdout, exit 0):

{"from":"zh","to":"en","source":"你好","translated":"Hello"}
FieldNote
-------------
fromAPI-detected language — may differ from --from auto
toTarget language
sourceOriginal input
translatedResult

Error (stderr, exit ≠ 0):

{"code":"AUTH_FAILED","message":"…","help_url":"https://…","raw_code":"52001"}

help_url and raw_code are omitted when not applicable.


Error Decision Tree

ExitcodeCauseAction
-----------------------------
0OKUse translated
1INVALID_INPUTEmpty textFix input, don't retry
1INVALID_LANGUAGEWrong lang code (e.g. ja → should be jp)Fix --to/--from, see Language Codes below
2CONFIG_MISSINGTRANS_API_KEY not setStop — run trans doctor --json then guide user to set up API key
2AUTH_FAILEDKey invalid/expired (raw_code 52001–52003)Stop — run trans doctor --json then guide user to API key page
3QUOTA_EXCEEDEDBalance = 0Stop — guide user to recharge (see below)
3RATE_LIMITEDQPS exceededWait 1 s, retry once
3API_ERRORUnexpected API errorRetry once; if persists, report raw_code + message
4NETWORK_ERRORNo connectivityRetry twice with 2 s gap; if persists, report

Exit 2 requires human intervention — the agent cannot fix auth or config.

Exit 1 means the agent passed bad arguments — fix the call, not the environment.


Language Codes

Baidu does not follow ISO 639. When unsure of a code, query locally:

trans languages                    # full list (200+ languages)
trans languages --filter jp        # search by code
trans languages --filter 日语      # search by name
trans languages --json             # JSON output for Agent parsing

The six most common ISO traps:

Baidu (correct)ISO (wrong for Baidu)Language
-------------------------------------------------
jpjaJapanese
korkoKorean
frafrFrench
spaesSpanish
araarArabic
vieviVietnamese

Other common codes that match ISO: zh cht en ru de pt it nl hi th

Use auto for source-language auto-detection.


Configuration

Priority: --api-key flag > TRANS_API_KEY env > ~/.trans-cli/config.json

Config file field: api_key.

trans config init               # create empty config skeleton (~/.trans-cli/config.json)
trans config init --force       # overwrite existing config
trans config set api_key <KEY>  # write api_key (creates file if absent)

Getting an API Key (First-Time Setup)

Guide the user through these steps — the agent cannot do this on their behalf:

  1. Open the Baidu Translation Open Platform and sign in:
  2. Go to Developer Center:
  3. Click "Use Now" → select "General Translation" → complete identity verification
  4. Get your API Key:
  5. Write to config: trans config set api_key

or set the environment variable TRANS_API_KEY

  1. Verify: trans doctor --jsonall_ok:true means success

Recharging (QUOTA_EXCEEDED)

Recharge portal:

Takes effect immediately — no restart needed. Auto-renewal can be enabled on the same page.

Usage details:


Non-Obvious Behaviours

  • Multiple positional args join with a space: trans text hello world"hello world"
  • When both stdin and positional args are given, positional args win silently
  • The from field in JSON is the API's detected language, not the --from flag value
  • trans doctor checks are short-circuit: if api_key fails, connectivity/account are skipped
  • --reference is optional; omitting it sends no extra field to the API

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-05-02 16:41 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,708
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 679 📥 327,859
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 72 📥 181,980