deepl/deepl

58 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# Defaults can be overridden in the config file
declare \
deepl_key='' \
default_target_lang='DE' \
deepl_host='api-free.deepl.com'
declare config_file="${XDG_CONFIG_HOME:-${HOME}/.config}/deepl/config"
if [ -r "$config_file" ]; then
# shellcheck source=/dev/null
source "$config_file"
fi
declare target_lang="$default_target_lang"
while getopts "hk:t:" arg; do
case $arg in
h)
echo 'Translate with Deepl'
echo 'https://codeberg.org/pierreprinetti/deepl'
echo
echo -e 'Usage:'
echo -e '\t-t: target language'
echo -e '\t-k: Deepl API key'
echo
echo -e 'Examples:'
echo -e $'\tprintf \'Hello, World!\' | deepl -k \'your-api-key\' -t PT'
echo -e '\tdeepl -t PT <<< "Hello, World"'
exit 0
;;
t)
target_lang=$OPTARG
;;
k)
deepl_key=$OPTARG
;;
*)
exit 1
;;
esac
done
readonly target_lang deepl_key
shift $((OPTIND-1))
urlencoded="$(jq -s -R -j @uri)"
curl \
-sS \
-X POST "https://${deepl_host}/v2/translate" \
-H "Authorization: DeepL-Auth-Key $deepl_key" \
-d "text=${urlencoded}" \
-d "target_lang=${target_lang}" \
| jq -j '.translations[0].text'