dotfiles/.local/bin/dmenugtranslate

39 lines
1.2 KiB
Plaintext
Raw Normal View History

2022-06-04 15:06:12 +02:00
#!/bin/sh
2022-06-04 16:45:27 +02:00
# Translate the current clipboard with the help of dmenu
#
# Utilized gtranslate. Install instructions here:
# https://git.sr.ht/~yerinalexey/gtranslate
# https://gitlab.com/-/snippets/2343500
2022-06-04 15:06:12 +02:00
text=$(xclip -o -selection clipboard)
map=~/.local/share/dabruh/gtranslate-lang-map
# Get user selection via dmenu from language map file.
from=$(cut -d '=' -f1 "$map" | dmenu -i -l 30 -p "Translate from")
[ -z "$from" ] && exit 1
from_code="$(grep "^$from=" "$map" | cut -d '=' -f2)"
to=$(cut -d '=' -f1 "$map" | grep -v Automatic | dmenu -i -l 30 -p "Translate to")
[ -z "$to" ] && exit 1
to_code="$(grep "^$to=" "$map" | cut -d '=' -f2)"
translation="$(curl --silent --get \
--data-urlencode "from=$from_code" \
--data-urlencode "to=$to_code" \
--data-urlencode "text=$text" \
"http://localhost:5000/api")"
rc=$?
if [ $rc -ne 0 ]; then
e="Translation from $from_code to $to_code failed. Input: '$text'"
notify-send "$e" &
echo "ERROR: $e" 1>&2
exit 2
fi
echo "$translation"
printf '%s' "$translation" | xclip -selection clipboard
notify-send "Translation from $from_code to $to_code copied to clipboard." &
alacritty --class translation -e bash -c "echo '$translation' | less" &