ambevar-dotfiles/.scripts/translate

76 lines
1.5 KiB
Plaintext
Raw Normal View History

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).
EOF
}
unset SL
while getopts ":hi:" opt; do
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" ;;
?)
_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'
fi 2>/dev/null | sed 's/\[\[\["\([^"]*\).*/\1/'
echo
}
if [ $# -ne 0 ]; then
_translate "$@"
else
_translate "$(cat)"
fi