Skip to content

Instantly share code, notes, and snippets.

@synsa
Created August 20, 2018 21:11
Show Gist options
  • Save synsa/7fd3aa79fe3ca0ec8cdd2af48fab1401 to your computer and use it in GitHub Desktop.
Save synsa/7fd3aa79fe3ca0ec8cdd2af48fab1401 to your computer and use it in GitHub Desktop.
Console Google Translate (bash + curl + sed)
#! /bin/bash
### Console Google Translate
### Example usage:
### gtran.sh en fr lovely spam
USAGE="Usage:
$0 en es Lovely spam!
Some codes: en|fr|de|ru|nl|it|es|ja|la|pl|bo
All language codes:
http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
FROM_LNG=$1
TO_LNG=$2
shift 2
QUERY=$*
UA="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803"
URL="http://translate.google.com.br/translate_a/t?client=t&hl=en&sl=$FROM_LNG&tl=$TO_LNG&otf=2&multires=1&ssel=0&tsel=0&sc=1"
curl --data-urlencode "text=$QUERY" -A $UA -s -g -4 $URL | sed 's/","/\n/g' | sed 's/\]\|\[\|"//g' | sed 's/","/\n/g' | sed 's/,[0-9]*/ /g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment