summaryrefslogtreecommitdiffstats
path: root/util/bin/google.translate
blob: a8ccd8cc7f0a3d198ca498883bca92229e074e27 (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
28
#! /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 'valid languages: https://cloud.google.com/translate/v2/using_rest#language-params'
  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 $*