#!/bin/sh # DEFAULT OPTIONS INSTANCE="https://translate.bus-hit.me" ENGINE="google" FLANG="auto" TLANG="en" BN="${0##*/}" errecho() { >&2 echo "$@" } fail() { errecho "error: $BN: $*" exit 1 } usage() { errecho \ "usage: $BN [-h] [-i INSTANCE] [-e ENGINE] [-f LANG] [-t LANG] TEXT translate text using simplytranslate -h show this help -i choose simplytranslate instance default: \"$INSTANCE\" -e use ENGINE to translate default: \"$ENGINE\" -f translate from LANG default: \"$FLANG\" -t translate to LANG default: \"$TLANG\"" exit 0 } # Read options while getopts :hi:f:t: OPT; do case "$OPT" in h) usage ;; i) INSTANCE="$OPTARG" ;; e) ENGINE="$OPTARG" ;; f) FLANG="$OPTARG" ;; t) TLANG="$OPTARG" ;; *) fail "unknown option: -$OPTARG. run $BN -h to see all options" ;; esac done shift $((OPTIND - 1)) [ -n "$*" ] || set -- "$(cat /dev/stdin)" [ -n "$*" ] || usage curl -s "${INSTANCE}/api/translate?engine=${ENGINE}&from=${FLANG}&to=${TLANG}&text=$(jq -rn --arg x "$*" '$x|@uri')" | jq -r '."translated-text"'