dotfiles/qutebrowser/data/userscripts/translate_google.sh

52 lines
1.4 KiB
Bash
Raw Normal View History

#!/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