#!/bin/bash

################################################################################
## Script para llamar a Google Translate.
## Álvaro Martín. Junio 2012.
##
##basado en http://crunchbanglinux.org/forums/topic/17034/access-google-translate-from-a-terminal/
##
################################################################################

#lista códigos idiomas:
    #http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

#google se encarga de identificar el lenguaje origen al estar en auto
#DEFAULT_SOURCE_LANG=auto
#DEFAULT_TARGET_LANG=es

DEFAULT_SOURCE_LANG=en
DEFAULT_TARGET_LANG=es


help='Usage: trans <text> [[<source language>] <target language>]
If no source language is specified autodetection will be used
If no target language is specified spanish will be used
Examples:
    trans love
        amor
    trans love fr
        amour
    trans lieben de en
        love'

if [ $# = 0 ]
then
    echo "trans: missing text"
    echo 'Usage: trans <text> [[<source language>] <target language>]'
    echo
    echo "Try 'trans -h' or trans --help' for options"
    exit
fi

if [ "$1" = -h ] || [ "$1" = --help ]
then
    echo "$help"
    exit
fi

if [ $3 ]; then
    source="$2"
    target="$3"
elif [ $2 ]; then
    source="$DEFAULT_SOURCE_LANG"
    target="$2"
else
    source="$DEFAULT_SOURCE_LANG"
    target="$DEFAULT_TARGET_LANG"
fi

#echo -n "($source) $1 -> ($target)"
result=$(wget  -U "Opera/7.50" -q -O - "http://translate.google.com/translate_a/t?client=t&text=$1&sl=$source&tl=$target" | cut -d \" -f2;)
echo " $result"

exit