dotfiles/qutebrowser/data/userscripts/translate_google.sh
Marty Oehme 8681d34946
qutebrowser: Make use of dotter for dir structure
Since we now use dotter we can simplify the dir structure for
qutebrowser a lot. Everything dot-filed earlier can now reside
in simple directories called config (for ~/.config/qutebrowser),
data (for ~/.local/share/qutebrowser), and scripts
(for ~/.local/bin) files.
2023-10-03 13:43:00 +02:00

51 lines
1.4 KiB
Bash
Executable file

#!/usr/bin/env sh
#
# Translate the page, or if `--text` argument is given the current selection, with google translate.
#
# Adapted code from https://github.com/AckslD/Qute-Translate, with much gratitude.
PAGE="https://lingva.garudalinux.org/SOURCELANGUAGE/TARGETLANGUAGE/TRANSLATETEXT"
while [ $# -gt 0 ]; do
case $1 in
-s | --source)
QUTE_TRANS_SOURCE=$2
shift
shift
;;
-t | --target)
QUTE_TRANS_TARGET=$2
shift
shift
;;
--url)
QUTE_TRANS_URL="true"
shift
;;
--text)
QUTE_TRANS_URL="false"
shift
;;
esac
done
if [ -z "$QUTE_TRANS_SOURCE" ]; then
# Default use automatic language for source
QUTE_TRANS_SOURCE="auto"
fi
if [ -z "$QUTE_TRANS_TARGET" ]; then
# Default use English for target
QUTE_TRANS_TARGET="en"
fi
if [ "$QUTE_TRANS_URL" = "false" ]; then
# Translate selected text
PAGE=$(echo "$PAGE" | sed -e "s/SOURCELANGUAGE/$QUTE_TRANS_SOURCE/" -e "s/TARGETLANGUAGE/$QUTE_TRANS_TARGET/" -e "s/TRANSLATETEXT/$QUTE_SELECTED_TEXT/")
echo "open -t ${PAGE}" >>"$QUTE_FIFO"
else
# Default translate URL
PAGE="https://translate.google.com/translate?"
CONT_KEY="u"
CONTENT="$QUTE_URL"
echo "open -t ${PAGE}sl=${QUTE_TRANS_SOURCE}&tl=${QUTE_TRANS_TARGET}&${CONT_KEY}=\"${CONTENT}\"" >>"$QUTE_FIFO"
fi