summaryrefslogtreecommitdiffstats
path: root/util/bin/translate.google
blob: 06c6a5a80a6561243e71ba9aff72c2efc5a1cbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /bin/sh
set -euf

sl=${SL:="auto"}
tl=${TL:="en"}

usage(){
  echo 'TL= targetlanguage, valid strings are in the style of "en", "pl" or "de". default "en"'
  echo 'SL= sourcelanguage, same TL, default is auto'
  echo 'Example: TL="de" ./translate.google "whats up?"'

  exit 0
}

translate(){
  text=$*
  url="http://translate.google.com/translate_a/t?client=t&sl=$sl&tl=$tl"
  curl -A "Mozilla/5.0" -Ss "$url" --data-urlencode "text=$text" |
    sed 's/\[\[\[\"//' |
    cut -d \" -f 1
}

if [ $# -eq 0 ]; then
  usage
fi

translate $*