Skip to content

Instantly share code, notes, and snippets.

@imShara
Created April 28, 2015 11:27
Show Gist options
  • Save imShara/0702cf4cf2de4ad58431 to your computer and use it in GitHub Desktop.
Save imShara/0702cf4cf2de4ad58431 to your computer and use it in GitHub Desktop.
Bash non-api context translation with Google Translate
#!/bin/bash
# Author: Shara
# Screenshot: http://i.imgur.com/pksgFLA.png
# Install:
# 1. Put to "/usr/local/sbin/translate"
# 2. Set "chmod +x translate"
# 3. Set hotkey with command "translate %LANG%" where %LANG% is result language
# Example:
# "translate en" to translate selected text to english
# "translate ru" to translate selected text to russian
# Set User-Agent string for request
USERAGENT="Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"
# Get selected text
SOURCETEXT=$(xsel -o)
# Get translation for selected text
TRANSLATION="$(wget -U "$USERAGENT" -qO - "https://translate.google.com/translate_a/single?client=t&sl=auto&dt=t&q=${SOURCETEXT}&tl=$1" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
# Put translation to clipboard
echo -n "$TRANSLATION" | xclip -filter -selection clipboard
# Show notify with source and translated text
notify-send -t 2500 "$SOURCETEXT" "$TRANSLATION"
@imShara
Copy link
Author

imShara commented Apr 28, 2015

Bash non-api context translation with Google Translate screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment