bin: Add trans to translate text with DeepL

This commit is contained in:
2022-09-19 14:21:12 +02:00
parent 12ef7cfa65
commit 4ccf7961b2

26
.local/bin/trans Executable file
View File

@@ -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; }