Created
April 28, 2015 11:27
-
-
Save imShara/0702cf4cf2de4ad58431 to your computer and use it in GitHub Desktop.
Bash non-api context translation with Google Translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Author
imShara
commented
Apr 28, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment