deepl-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

deepl-cli

deepl-cli

CLI tool for translating text using the DeepL API. Pipe-friendly, supports context and formality options.
一款调用DeepL API进行文本翻译的CLI工具。支持管道操作,可配置上下文和语体风格选项。

Prerequisites

前置要求

Installation

安装

bash
npm install -g @daliusd/deepl-cli
bash
npm install -g @daliusd/deepl-cli

Configuration

配置

Create
~/.config/deepl-cli/config.json
:
bash
mkdir -p ~/.config/deepl-cli
Option A -- Static API key:
json
{
  "api_key": "your-deepl-api-key"
}
Option B -- Command-based key (for password managers like
pass
, 1Password CLI, etc.):
json
{
  "api_key_command": "pass show deepl-api-key"
}
If both are present,
api_key_command
takes precedence.
创建
~/.config/deepl-cli/config.json
bash
mkdir -p ~/.config/deepl-cli
选项A -- 静态API密钥:
json
{
  "api_key": "your-deepl-api-key"
}
选项B -- 命令式密钥(适用于
pass
、1Password CLI等密码管理器):
json
{
  "api_key_command": "pass show deepl-api-key"
}
如果同时配置了两者,
api_key_command
优先级更高。

Commands

命令

All translation is done via a single command:
deepl-cli [options] [text]
所有翻译操作都通过单个命令完成:
deepl-cli [options] [text]

Basic Translation

基础翻译

bash
deepl-cli -t de "Hello, world!"
bash
deepl-cli -t de "Hello, world!"

Specify Source Language

指定源语言

bash
deepl-cli -t de -s en "Hello"
By default the source language is auto-detected. Use
-s
only when auto-detection is insufficient.
bash
deepl-cli -t de -s en "Hello"
默认会自动检测源语言。仅当自动检测结果不准确时才需要使用
-s
参数。

Context-Aware Translation

上下文感知翻译

Context influences the translation but is not itself translated or billed:
bash
deepl-cli -t de -c "This is a greeting in a formal business email" "Hello"
上下文会影响翻译结果,但本身不会被翻译或计入计费字符:
bash
deepl-cli -t de -c "This is a greeting in a formal business email" "Hello"

Formality Control

语体风格控制

bash
undefined
bash
undefined

Formal

正式语体

deepl-cli -t de -f more "How are you?"
deepl-cli -t de -f more "How are you?"

Informal

非正式语体

deepl-cli -t de -f less "How are you?"

Valid formality values: `less`, `more`, `default`, `prefer_less`, `prefer_more`.
deepl-cli -t de -f less "How are you?"

有效的语体风格值:`less`、`more`、`default`、`prefer_less`、`prefer_more`。

Piped Input

管道输入

bash
echo "Hello, world!" | deepl-cli -t de
Translated text goes to stdout with no extra formatting, making it pipeline-friendly:
bash
echo "Hello" | deepl-cli -t de | pbcopy
bash
echo "Hello, world!" | deepl-cli -t de
翻译后的文本会输出到标准输出(stdout),无额外格式,便于在管道中使用:
bash
echo "Hello" | deepl-cli -t de | pbcopy

Verbose Mode

详细模式

Metadata is written to stderr so stdout remains clean:
bash
deepl-cli -t de -v "Hello, world!"
元数据会写入标准错误输出(stderr),确保标准输出(stdout)内容纯净:
bash
deepl-cli -t de -v "Hello, world!"

stdout: Hallo, Welt!

stdout: Hallo, Welt!

stderr: Detected source language: en

stderr: Detected source language: en

stderr: Billed characters: 13

stderr: Billed characters: 13

undefined
undefined

Options Reference

选项参考

OptionShortDescription
--target <lang>
-t
Target language code (required, e.g.
de
,
en-US
,
fr
)
--source <lang>
-s
Source language code (default: auto-detect)
--context <text>
-c
Additional context for translation (not translated, not billed)
--formality <level>
-f
Formality:
less
,
more
,
default
,
prefer_less
,
prefer_more
--verbose
-v
Show metadata (detected source language, billed characters)
--help
-h
Show help
--version
Show version
选项简写描述
--target <lang>
-t
目标语言代码(必填,例如
de
en-US
fr
--source <lang>
-s
源语言代码(默认:自动检测)
--context <text>
-c
翻译的额外上下文(不会被翻译,不计入计费)
--formality <level>
-f
语体风格:
less
more
default
prefer_less
prefer_more
--verbose
-v
显示元数据(检测到的源语言、计费字符数)
--help
-h
显示帮助信息
--version
显示版本号

Common Language Codes

常用语言代码

Language codes are case-insensitive and follow ISO 639-1.
Source languages (or omit for auto-detection):
CodeLanguage
en
English
de
German
fr
French
es
Spanish
it
Italian
ja
Japanese
zh
Chinese
pl
Polish
nl
Dutch
pt
Portuguese
ru
Russian
Target languages (some include regional variants):
CodeLanguage
en-US
English (American)
en-GB
English (British)
de
German
fr
French
es
Spanish
it
Italian
ja
Japanese
zh-Hans
Chinese (Simplified)
zh-Hant
Chinese (Traditional)
pl
Polish
nl
Dutch
pt-BR
Portuguese (Brazilian)
pt-PT
Portuguese (European)
ru
Russian
See the DeepL API documentation for the full list.
语言代码不区分大小写,遵循ISO 639-1标准。
源语言(或留空以自动检测):
代码语言
en
英语
de
德语
fr
法语
es
西班牙语
it
意大利语
ja
日语
zh
中文
pl
波兰语
nl
荷兰语
pt
葡萄牙语
ru
俄语
目标语言(部分包含区域变体):
代码语言
en-US
英语(美式)
en-GB
英语(英式)
de
德语
fr
法语
es
西班牙语
it
意大利语
ja
日语
zh-Hans
中文(简体)
zh-Hant
中文(繁体)
pl
波兰语
nl
荷兰语
pt-BR
葡萄牙语(巴西)
pt-PT
葡萄牙语(欧洲)
ru
俄语
完整列表请查看DeepL API文档

Tips

小贴士

  • No arguments or
    --help
    shows usage information.
  • Multiple words as positional arguments are joined with a space:
    deepl-cli -t de Hello world
    translates "Hello world".
  • Verbose mode writes metadata to stderr, so piping stdout is always safe.
  • api_key_command
    is recommended over
    api_key
    to avoid storing secrets in plain text.
  • 不带参数或使用
    --help
    会显示使用说明。
  • 多个单词作为位置参数时会自动用空格连接:
    deepl-cli -t de Hello world
    会翻译“Hello world”。
  • 详细模式会将元数据写入stderr,因此管道传输stdout始终是安全的。
  • 推荐使用
    api_key_command
    而非
    api_key
    ,避免以明文形式存储密钥。