#!/bin/sh # # l'autre babelfish: translate # the other babelfish: translate # # Depends on "curl", by Daniel Stenberg # # usage: translate "Where's the form? Don't ask. Don't think. [...]" # translate fr_en "Si une horloge était conscinete d'elle même [...]" # translate UTF8 fr_de "Les petits oiseaux gazouillent dans les arbres" # # Copypouet þ Benoît Rouits # Inspired from 'babelfish', by Thomas Nemeth. PROG=`basename $0` VERSION="0.4" #2008/01/20 DEBUG=0 if [ "$1" = "--debug" ]; then DEBUG=1 shift fi MY=`echo $LANG | cut -d"_" -f 1` #MY is taken as your expected natural language #i hope this works for all countries... if [ $MY = "C" -o $MY = "" ] then MY="en" #as you can see, your language defaults to English if not guessed or given. fi [ "$DEBUG" = "1" ] && echo "DEBUG => MY = ${MY}" #############beginning of FROM/TO default configuration########## FROM="en" #set your prefered FROM to be more convenient for you TO="$MY" #but you can obviously switch the defaults... #############end of default FROM/TO configuration################ FROMTO="${FROM}_$TO" [ "$DEBUG" = "1" ] && echo "DEBUG => FROMTO = $FROMTO" #############beginning of TEXT ENCODING default configuration########## TEXT="If a clock was conscious of itself, it could never keep time. --K.Werner" ENCODING="`echo $LANG | sed -e 's/.._...\(.*\)/\1/'`" #set your defaults here #############end of default TEXT ENCODING configuration################ ENCODSTYPES="us-ascii iso8859-1 iso8859-2 iso8859-3 iso8859-4 iso8859-5 iso8859-iso8859-6 iso8859-7 iso8859-8 iso8859-9 iso8859-10 iso8859-11 iso8859-12 iso8859-13 ido8859-14 iso8859-15 UTF8" TRADUCSTYPES="en_de en_es en_fr en_it en_pt en_ja en_ko en_zh de_en de_fr es_en fr_en fr_de it_en pt_en ja_en ko_en zh_en" if [ "$1" = "--help" -o "$1" = "-h" ] then echo "$PROG v$VERSION" echo "Usage : $PROG [encodage] [direction] mots" echo "Exemple d'encodage : iso8859-1, UTF8 ... (defaut : $ENCODING)" echo "Direction de traduction (défaut : $FROMTO) :" echo "en_de Anglais vers Allemand" echo "en_es Anglais vers Espagnol" echo "en_fr Anglais vers Français" echo "en_it Anglais vers Italien" echo "en_pt Anglais vers Portugais" echo "en_ja Anglais vers Japonais" echo "en_ko Anglais vers Koréen" echo "en_zh Anglais vers Chinois" echo "de_en Allemand vers Anglais" echo "de_fr Allemand vers Français" echo "es_en Espagnol vers Anglais" echo "fr_en Français vers Anglais" echo "fr_de Français vers Allemand" echo "it_en Italien vers Anglais" echo "pt_en Portugais vers Anglais" echo "ja_en Japonais vers Anglais" echo "ko_en Koréen vers Anglais" echo "zh_en Chinois vers Anglais" exit 0 fi for enc in $ENCODSTYPES do if [ "$enc" = "$1" ] then ENCODING="$enc" shift fi done [ "$DEBUG" = "1" ] && echo "DEBUG => ENCODING = $ENCODING" for ttyp in $TRADUCSTYPES do if [ "$ttyp" = "$1" ] then FROMTO="$ttyp" [ "$DEBUG" = "1" ] && echo "DEBUG => FROMTO = $FROMTO" shift fi done [ $# -gt 0 ] && TEXT="$*" [ "$DEBUG" = "1" ] && echo "DEBUG => TEXT = $TEXT" DIRECTION=`echo "$FROMTO" | sed -e 's/\_/ -> /'` [ "$DEBUG" = "1" ] && echo "DEBUG => DIRECTION = $DIRECTION" TEXTURL=`echo "$TEXT" | sed -e 's/ /\+/g'` [ "$DEBUG" = "1" ] && echo "DEBUG => TEXTURL = \"${TEXTURL}\"" #google specific: URL="http://translate.google.com/translate_t" [ "$DEBUG" = "1" ] && echo "DEBUG => URL = \"${URL}\"" FROMTO=`echo "$FROMTO" | sed -e 's/\_/|/'` [ "$DEBUG" = "1" ] && echo "DEBUG => FROMTO = \"${FROMTO}\"" #-d "oe=$ENCODING" \ no more "oe" found in Jan 2007 version [ "$DEBUG" = "1" ] && echo "DEBUG => curl -s -A \"Mozilla/1.0 (compatible; translate/$VERSION)\" -d \"hl=$MY\" -d \"ie=$ENCODING\" -d \"langpair=$FROMTO\" -d \"text=$TEXTURL\" \"$URL\" > $HOME/.translate.tmp" curl -s -A "Mozilla/1.0 (compatible; translate/$VERSION)" \ -d "hl=$MY" -d "ie=$ENCODING" \ -d "langpair=$FROMTO" \ -d "text=$TEXTURL" \ "$URL" | grep result_box | sed -e '1d' | cut -b 1700-3000 | sed -e "s/'/'/g" > "$HOME"/.translate.tmp LANG=C sed -e 's?^.*id=result_box dir="ltr">\([^<]*\).*?\1?' < "$HOME"/.translate.tmp | iconv --from latin1 --to $ENCODING rm $HOME/.translate.tmp