From 4ccf7961b2257262cfa9c3f30e00496f1d96387d Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Mon, 19 Sep 2022 14:21:12 +0200 Subject: [PATCH] bin: Add `trans` to translate text with DeepL --- .local/bin/trans | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 .local/bin/trans diff --git a/.local/bin/trans b/.local/bin/trans new file mode 100755 index 0000000..30a5372 --- /dev/null +++ b/.local/bin/trans @@ -0,0 +1,26 @@ +#!/bin/sh + +# Translate given text passed via stdin with the help of DeepL. Expects the API +# token to be passed through the environment variable $DEEPL_API_TOKEN. +# +# The output is only the translated text if `jq` is installed, and the json +# response from DeepL otherwise. +# +# Assumes German as source language and English (US) as target language if not +# specified via the first two arguments. +# +# Can be simply used in vim: +# +# :'<,'>!trans + +ENDPOINT='https://api-free.deepl.com/v2/translate' + +source="${1:-DE}" +target="${1:-EN-US}" + +curl -sS "$ENDPOINT" \ + -H "Authorization: DeepL-Auth-Key $DEEPL_API_TOKEN" \ + --data-urlencode "text=$(cat)" \ + -d "source_lang=$source" \ + -d "target_lang=$target" \ +| { command -v jq >/dev/null 2>&1 && jq -r '.translations[].text' || cat; }