2022-06-05 00:05:57 +02:00
|
|
|
#!/bin/bash
|
2022-06-04 15:06:12 +02:00
|
|
|
|
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
|
2022-06-05 00:05:57 +02:00
|
|
|
FILE="/tmp/dmenugtranslate"
|
|
|
|
|
|
|
|
from="$(cut -d, -f1 2>/dev/null <"$FILE")"
|
|
|
|
to="$(cut -d, -f2 2>/dev/null <"$FILE")"
|
|
|
|
|
|
|
|
[ -n "$from" ] && LAST_FROM="$from"
|
|
|
|
[ -n "$to" ] && LAST_TO="$to"
|
|
|
|
|
|
|
|
# Inject the last used value on top. Remove the auto option for "Translate to".
|
|
|
|
FROM_MAP="$(echo "$LAST_FROM" | cat - "$map" | grep -v "^$" | cut -d= -f1 | uniq)"
|
|
|
|
TO_MAP="$(echo "$LAST_TO" | cat - "$map" | grep -Ev "^(Automatic=|$)" | cut -d= -f1 | uniq)"
|
2022-06-04 15:06:12 +02:00
|
|
|
|
|
|
|
# Get user selection via dmenu from language map file.
|
2022-06-05 00:05:57 +02:00
|
|
|
from=$(dmenu -i -l 30 -p "Translate from" <<<"$FROM_MAP")
|
2022-06-04 15:06:12 +02:00
|
|
|
[ -z "$from" ] && exit 1
|
|
|
|
from_code="$(grep "^$from=" "$map" | cut -d '=' -f2)"
|
|
|
|
|
2022-06-05 00:05:57 +02:00
|
|
|
to=$(dmenu -i -l 30 -p "Translate to" <<<"$TO_MAP")
|
2022-06-04 15:06:12 +02:00
|
|
|
[ -z "$to" ] && exit 1
|
|
|
|
to_code="$(grep "^$to=" "$map" | cut -d '=' -f2)"
|
|
|
|
|
2022-06-05 00:05:57 +02:00
|
|
|
echo "$from=$from_code,$to=$to_code" >"$FILE"
|
|
|
|
|
2022-06-04 15:06:12 +02:00
|
|
|
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" &
|