DeepL command line tool in Crystal

I created a simple tool deepl-cli that calls the DeepL API from the command line.

This tool is especially useful for non-native English users like me. It can translate man pages, help, or logs.

(although the removal of ANSI escape sequences is not perfect)

Crest made it easy to implement features like PROXY support and HTTP multipart requests. The lack of PROXY and document translation support in many existing DeepL command line tools motivated the development of this tool. (I simply wanted to write Crystal)

As for distribution, I had no problem with Linux and Windows, but on Mac, I couldn’t figure out how to link statically, so I used homebrew tap.

7 Likes

I was created translater for almost same purpose, although, i use selenium drive firefox to visit public web translate service for this purpose

But, i am current using llama 3.2 for translate for now, it read content from system primary clipboard, and print translated content out use wezterm console, like following.

basically, the wrapper script just run command like following:

content="hello world"

ollama run llama3.2 "Translate the following content from English to Chinese without additional explanation:
$content"

the big advantage is, it give me the result always immediately, even without need network connection.

Oh, that’s a great idea.

I created this tool for the personal purpose of translating English academic papers and technical texts such as help for command line tools.
It is for people who

  1. who use Unix command line tools
  2. who like to pipe standard output and standard input
  3. are willing to pay for accurate translations
  4. would like the output to be accurate and have fewer errors than to be a readable masterpiece.
  5. would like the PDF to be translated.
  6. want to use it in a proxy environment.

In fact, not many people prefer the Unix command line interface, making this tool very targeted.

Tools unconsciously have a strong projection of the creator’s needs. However, this may only become apparent by comparing your tool with other tools.

deepl-cli is all about practicality, and from a technical point of view, it is not particularly interesting.

Yes, it would be more interesting to make use of local LLM. (From a computer geek’s point of view)

I use CLI a lot too.

  • would like the PDF to be translated.

If support read content from system primary clipboard, you almost can translate anythings if use on linux, following is a bash script you can reference:

if [[ $XDG_SESSION_TYPE == "wayland" ]]; then
    ai_translater "$(wl-paste -n -p)"
else
    ai_translater "$(xclip -selection primary -o)"
fi

It use wl-paste for Wayland and xclip for Xorg to read content when you mark a selection of content use mouse, then translate it use a global keybinding, I even add a firefox extention for use ollama translate from mouse right menu.

PDF translation is actually more difficult than you might imagine. This is because you need to identify the layout using OCR, and you need to either adjust the length of the original text before translation or the size of the characters after translation. Otherwise, the layout will easily fall apart.

In fact DeepL translation is far from perfect. So, I don’t actually use PDF translation that often. It’s also expensive. There are many AI tools that convert PDFs to markdown, but I don’t think any of them are perfect yet.

deepl-cli also provides simple clipboard support. This is the same as your idea of executing external commands

@zw963
On my Ubuntu 24.04, xsel works (thanks to XWayland?), but it looks like I should change to wl-paste. Thanks.

In fact, not same, i saw your shards use xsel --clipboard which copy from system clipboard, which need user do an exact a Copy operation(as shortcut Ctrl +Shift + V does), but, it more conveniently if copy from primary (it is xsel default),
you only need select and highlight the content, as following screenshot, it will copy to primary automatically, and not mess up the system clipboard.


1 Like

Oh, I see. There is something called the “Primary Clipboard”. I didn’t know about this.