Translate text using Baidu AI Translation API via trans-cli with Baidu-specific language codes, API key management, error handling, and environment diagnostics.
---
name: baidu-text-translate
description: Use this skill whenever you need to translate text with the trans-cli tool (Baidu Translation AI API). Covers JSON output contract, Baidu-specific language codes (jp/kor/fra/spa/ara — not ISO defaults), exit code semantics, error decision tree, first-time API key setup, QUOTA_EXCEEDED / AUTH_FAILED recovery, environment self-diagnosis via `trans doctor`, and listing supported languages via `trans languages`. Trigger on any of: running `trans text`, shell pipelines with translation, trans-cli errors, API key configuration, `trans doctor` checks, or looking up language codes.
homepage: https://fanyi.baidu.com
metadata: {"clawdbot":{"emoji":"🌐","requires":{"bins":["trans"],"env":["TRANS_API_KEY"]},"install":[{"id":"npm","kind":"npm","package":"@bdtrans/trans-cli","bins":["trans"],"label":"Install trans-cli (npm)"}]}}
---
# 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.
```bash
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`:
```bash
trans doctor # human-readable output
trans doctor --json # JSON output for Agent parsing
```
**JSON contract** (stdout):
```json
{
"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
}
```
| Exit | `all_ok` | Meaning |
|------|----------|---------|
| 0 | `true` | All checks passed — ready to translate |
| 2 | `false` | At 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):
```json
{"from":"zh","to":"en","source":"你好","translated":"Hello"}
```
| Field | Note |
|-------|------|
| `from` | API-detected language — may differ from `--from auto` |
| `to` | Target language |
| `source` | Original input |
| `translated` | Result |
**Error** (stderr, exit ≠ 0):
```json
{"code":"AUTH_FAILED","message":"…","help_url":"https://…","raw_code":"52001"}
```
`help_url` and `raw_code` are omitted when not applicable.
---
## Error Decision Tree
| Exit | `code` | Cause | Action |
|------|--------|-------|--------|
| 0 | — | OK | Use `translated` |
| 1 | `INVALID_INPUT` | Empty text | Fix input, don't retry |
| 1 | `INVALID_LANGUAGE` | Wrong lang code (e.g. `ja` → should be `jp`) | Fix `--to`/`--from`, see Language Codes below |
| 2 | `CONFIG_MISSING` | `TRANS_API_KEY` not set | Stop — run `trans doctor --json` then guide user to set up API key |
| 2 | `AUTH_FAILED` | Key invalid/expired (`raw_code` 52001–52003) | Stop — run `trans doctor --json` then guide user to API key page |
| 3 | `QUOTA_EXCEEDED` | Balance = 0 | Stop — guide user to recharge (see below) |
| 3 | `RATE_LIMITED` | QPS exceeded | Wait 1 s, retry once |
| 3 | `API_ERROR` | Unexpected API error | Retry once; if persists, report `raw_code` + `message` |
| 4 | `NETWORK_ERROR` | No connectivity | Retry 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:
```bash
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 |
|-----------------|----------------------|----------|
| `jp` | `ja` | Japanese |
| `kor` | `ko` | Korean |
| `fra` | `fr` | French |
| `spa` | `es` | Spanish |
| `ara` | `ar` | Arabic |
| `vie` | `vi` | Vietnamese |
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`.
```bash
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: <https://fanyi-api.baidu.com/>
2. Go to Developer Center: <https://fanyi-api.baidu.com/manage/developer>
3. Click "Use Now" → select "General Translation" → complete identity verification
4. Get your API Key: <https://fanyi-api.baidu.com/manage/apiKey>
5. Write to config: `trans config set api_key <KEY>`
or set the environment variable `TRANS_API_KEY`
6. Verify: `trans doctor --json` — `all_ok:true` means success
---
## Recharging (QUOTA_EXCEEDED)
Recharge portal: <https://fanyi-api.baidu.com/manage/account>
Takes effect immediately — no restart needed. Auto-renewal can be enabled on the same page.
Usage details: <https://fanyi-api.baidu.com/api/trans/user/usage>
---
## 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
don't have the plugin yet? install it then click "run inline in claude" again.
expanded original into six required sections with explicit step-by-step procedure, decision tree for all exit codes and error conditions, structured output contract with json examples, environment input/connection documentation with setup urls, and outcome signals for each success/failure path.
use this skill to translate text via the baidu ai translation api when you need to convert content between languages. the trans command wraps baidu's api and outputs structured json for parsing. trigger this skill whenever translating text, setting up api keys for the first time, debugging translation failures, checking environment health, or looking up baidu-specific language codes (which do not follow iso 639 standards).
external connection: baidu ai translation api
TRANS_API_KEY (or stored in ~/.trans-cli/config.json under api_key field)binary requirement
trans command (installed via npm: npm install -g @bdtrans/trans-cli)parameters for translation
auto for auto-detect; baidu codes differ from iso 639, see language codes table below)environment context
input: none (diagnostic step)
action: run trans doctor --json to check api key, connectivity, and account status before any translation attempt
output: json object with checks array and all_ok boolean; exit code 0 if all checks pass, 2 if any fails
parse the result:
all_ok: true, proceed to step 2all_ok: false, inspect checks[].ok and checks[].message; identify which check failed (api_key, connectivity, or account)input: text string, source language code (or auto), target language code, optional reference instruction
action: execute trans text --json --from <SOURCE> --to <TARGET> [--reference "<INSTRUCTION>"] "<TEXT>" or pipe via echo/stdin
output: json object on stdout with fields from, to, source, translated; or json error object on stderr with code, message, help_url, raw_code
exit code: 0 on success, non-zero on failure (see decision points)
examples:
trans text --json --to en "你好世界"
trans text --json --from zh --to jp --reference "使用正式语气" "你好"
echo "早上好" | trans text --json --to en
trans text --json --to fra "bonjour"
input: user has baidu account but no api key configured action: guide user through these manual steps (agent cannot perform on behalf):
trans config set api_key <KEY> (creates ~/.trans-cli/config.json if absent)export TRANS_API_KEY=<KEY>trans doctor --json and confirm all_ok: true
output: api key configured; doctor command exits 0input: quota_exceeded error (exit code 3) from translation attempt action: guide user to https://fanyi-api.baidu.com/manage/account to recharge balance (takes effect immediately, no restart required); user can enable auto-renewal on same page output: account recharged; retry translation from step 2
input: error code invalid_language or uncertainty about baidu language codes
action: query supported languages locally via trans languages --json [--filter <KEYWORD>]
output: json array of language objects with code, name, and aliases; or run without --json for human-readable table
examples:
trans languages --json # all 200+ languages
trans languages --filter jp # search by code
trans languages --filter 日语 # search by name
reference the common iso traps in the table below if code is wrong.
input: non-zero exit code from trans command
action: parse json stderr to extract code field; dispatch to appropriate handler (see decision points section)
output: error code and message identified; action taken (retry, fix input, configure auth, etc.)
if exit code is 0
translated field from stdout json.if exit code is 1 (invalid input or invalid language)
code: INVALID_INPUT: input text is empty or malformed. fix the text and retry; do not retry the same call.code: INVALID_LANGUAGE: source or target language code is wrong (often iso 639 mistake like ja instead of jp for japanese). check language codes section and run trans languages --filter <CODE> to verify; fix --from or --to flag and retry.if exit code is 2 (config or auth failure)
code: CONFIG_MISSING: environment variable TRANS_API_KEY is not set and no config file exists. stop. run trans doctor --json to diagnose; guide user through step 3a (first-time setup).code: AUTH_FAILED with raw_code in range 52001-52003: api key is invalid, expired, or revoked. stop. run trans doctor --json to confirm account check failure; guide user to https://fanyi-api.baidu.com/manage/apiKey to refresh key, then re-run trans config set api_key <NEW_KEY> and step 2.if exit code is 3 (quota or rate limit)
code: QUOTA_EXCEEDED: user balance is zero. stop. guide user through step 3b (recharge at https://fanyi-api.baidu.com/manage/account).code: RATE_LIMITED: qps (queries per second) limit exceeded. wait 1 second and retry once; if second attempt also rate-limits, report to user that api is under heavy load.code: API_ERROR with raw_code: unexpected baidu api error. retry once immediately; if error persists on second attempt, report raw_code and message to user for investigation or escalation.if exit code is 4 (network error)
code: NETWORK_ERROR: no connectivity to baidu api. retry twice with 2 second gap between retries; if both retries fail, report network connectivity issue and suggest checking firewall/proxy/internet connection.if doc/list/config commands are used instead of translation
trans doctor --json: environment diagnostic (step 1; exit 0 if all_ok, 2 if any check fails)trans languages --json: language code lookup (step 4; exit 0 always)trans config set api_key <KEY>: api key configuration (step 3a; exit 0 on success)success case (exit 0)
from (detected source language code), to (target language code), source (original input string), translated (translated output string){"from":"zh","to":"en","source":"你好","translated":"Hello"}error case (exit 1, 2, 3, or 4)
code (error code string), message (human-readable error description); may include optional fields help_url (link to fix) and raw_code (baidu api raw error code){"code":"AUTH_FAILED","message":"invalid api key","help_url":"https://fanyi-api.baidu.com/manage/apiKey","raw_code":"52001"}doctor diagnostic (exit 0 or 2)
checks array (each check has name, ok boolean, message, and optional latency_ms or help_url) and all_ok boolean{
"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
}
language list (exit 0)
code, name, and optional aliasestranslation succeeded
translated field matching expected target languagefrom field to see which source language baidu detectedenvironment is healthy
trans doctor --json returns all_ok: true and exit 0ok: trueapi key is configured
trans config set api_key <KEY> exits 0trans doctor --json call has api_key.ok: truequota needs recharge
code: QUOTA_EXCEEDEDlanguage code was corrected
trans languages --filter <KEYWORD> returns matching language codenetwork or auth issue identified
trans doctor --json pinpoints which check failed (connectivity, api_key, or account) with ok: false and descriptive message