#!/bin/sh
# $Id: translate,v 1.3 2004-02-06 00:31:05 ianb-guest Exp $
# elvis: translate	-- Translate human languages (various providers)
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_translate_provider	babelfish
def   SURFRAW_translate_from		fr
def   SURFRAW_translate_to		en
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [words-to-translate]...
Description:
  Surfraw translate human language
Local options:
  -from=LANGUAGE		Souce language
  				Default: $SURFRAW_translate_from
				Environment: SURFRAW_translate_from
  -to=LANGUAGE			Destination language
  				Default: $SURFRAW_translate_to
				Environment: SURFRAW_translate_to
  -provider=	                Translation service provider
            babelfish		babelfish.altavista.com (systrans)
	    raging		babelfish.altavista.com/raging (systrans)
                                Default: $SURFRAW_translate_provider
				Environment: SURFRAW_translate_provider
Languages:
  de				German
  en				English
  es				Spanish
  fr				French
  it				Italian
  ja				Japanese
  ko				Korean
  pt				Portuguese
  ru				Russian
  zh				Chinese

Examples:
  $w3_argv0 			Provider's standard translation page
  $w3_argv0 -provider=raging	Use raging search as a translation provider
  $w3_argv0 -from=fr -to=en la verite vous liberera
Bugs:
  Systrans derived providers only support the following translation
  combinations:
	English to Chinese
	English to French
	English to German
	English to Italian
	English to Japanese
	English to Korean
	English to Portuguese
	English to Spanish
	Chinese to English
	French to English
	French to German
	German to English
	German to French
	Italian to English
	Japanese to English
	Korean to English
	Portuguese to English
	Russian to English
	Spanish to English
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
	-from=*)	setopt SURFRAW_translate_from		$optarg ;;
	-to=*)		setopt SURFRAW_translate_to		$optarg ;;
	-provider=*)	setopt SURFRAW_translate_provider	$optarg	;;
	*) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments
if test -z "$w3_args"
then
	w3_browse_url "http://babelfish.altavista.com/"
else
	escaped_args=`w3_url_of_arg $w3_args`
	case "$escaped_args" in
		http*) w3_browse_url "http://babelfish.altavista.com/babelfish/urltrurl?tt=url&url=${escaped_args}&lp=${SURFRAW_translate_from}_${SURFRAW_translate_to}"
		;;
		*) w3_browse_url "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=1&urltext=${escaped_args}&lp=${SURFRAW_translate_from}_${SURFRAW_translate_to}"
        ;;
    esac
fi

exit $?
