2013-10-06 19:54:15 +02:00
#!/bin/sh
## Original command:
# curl -A "Mozilla/5.0" 'http://translate.google.com/translate_a/t?client=t&text=hello&hl=en&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&multires=1&prev=btn&ssel=0&tsel=0&sc=1' | sed 's/\[\[\["\([^"]*\).*/\1/'
_printhelp ()
{
cat<<EOF
Usage: ${1##*/} [-i source-language] target-language [text]
The language are specified using the international prefix (e.g. en for English,
de for German, etc.).
When omitting the source language, the service tries to guess the language from
the input content.
When omitting the text, standard input is read.
Synopsis.
-h: Show this help.
-i: input language (optional).
2013-10-07 00:09:15 +02:00
-l: show alternate translations.
2013-10-06 19:54:15 +02:00
EOF
}
2013-10-07 00:09:15 +02:00
FILTER='s/\[\[\["\([^"]*\).*/\1/'
2013-10-06 19:54:15 +02:00
unset SL
2013-10-07 00:09:15 +02:00
while getopts ":hi:l" opt; do
2013-10-06 19:54:15 +02:00
case $opt in
h)
_printhelp "$0"
2013-10-06 19:56:55 +02:00
exit 1
2013-10-06 19:54:15 +02:00
;;
i)
SL="$OPTARG" ;;
2013-10-07 00:09:15 +02:00
l)
FILTER='s/\[\[\[[^[]*\[\[[^[]*\[\([^[]*\)\].*/\1/' ;;
2013-10-06 19:54:15 +02:00
?)
_printhelp "$0"
2013-10-06 19:56:55 +02:00
exit 1
2013-10-06 19:54:15 +02:00
;;
:)
_printhelp "$0"
2013-10-06 19:56:55 +02:00
exit 1
2013-10-06 19:54:15 +02:00
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
2013-10-06 19:56:55 +02:00
exit 1
2013-10-06 19:54:15 +02:00
fi
TL=$1
shift
_translate()
{
TEXT="$(echo "$@" | tr '\n' ' ' | sed 's/[\t ]\+/%20/g')"
if [ "$SL" = "" ]; then
curl -A "Mozilla/5.0" 'http://translate.google.com/translate_a/t?client=t&text='$TEXT'&tl='$TL'&ie=UTF-8&oe=UTF-8'
else
curl -A "Mozilla/5.0" 'http://translate.google.com/translate_a/t?client=t&text='$TEXT'&sl='$SL'&tl='$TL'&ie=UTF-8&oe=UTF-8'
2013-10-07 00:09:15 +02:00
fi 2>/dev/null | sed -e $FILTER -e 's/"//g'
2013-10-06 19:54:15 +02:00
echo
}
if [ $# -ne 0 ]; then
_translate "$@"
else
_translate "$(cat)"
fi