Skip to content

Instantly share code, notes, and snippets.

@sineemore
Created April 13, 2018 14:18
Show Gist options
  • Save sineemore/f707e414d60914b1d8d627d8fd02436d to your computer and use it in GitHub Desktop.
Save sineemore/f707e414d60914b1d8d627d8fd02436d to your computer and use it in GitHub Desktop.
Translate selection and view it with notify-send
#!/bin/sh
# translate-xsel - translate clipboard via "Google Translate"
die() {
printf 'translate-xsel: %s' "$1" >&2
notify-send "translate-xsel" "$1"
exit 1
}
sl='auto'
tl='en'
src=$(xclip -o)
[ $? -ne 0 ] && die 'xclip retcode is not 0 (do you have xclip?)'
[ -z "$src" ] && die 'Empty clipboard, nothing to translate'
raw=$(wget -U "Mozilla/5.0" -qO- "http://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${src}")
[ $? -ne 0 ] && die 'wget retcode is not 0'
translated=$(printf '%s' "$raw" | jq -r '.[0]?[0]?[0]? // .')
[ -n "$translated" ] || die "Not translated"
notify-send "translate-xsel" "${src}\n${translated}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment