#!/bin/sh ## TODO: support for long texts. _usage () { cat</dev/null 2>&1; then ## Input may have empty fields, which is not normally allowed in ## json. Let's fix this. input="$(sed -e 's/,\+/,/g' -e 's/\[,\+/\[/g' -e 's/,\+\]/\]/g')" ## The use of jshon without checking the array size is unsafe. If the ## size is not as expected, it probably means that source and target ## languages are the same. Anyhow, we drop the error output. ## Google Translate does not have the same structure when English is ## involved. With English, the list of translations is in cell 1, ## whitout English it is in cell 3. Without English, there is no ## distinction on grammatical type. if [ "$(echo "$input" | jshon -e 1 -t)" = "string" ]; then ## No English echo "$input" | jshon -e 3 -e 0 -e 2 -a -e 0 -u | tr '\n' ',' | sed 's/,$/\n/' else count=0 ## Fetch grammatical type in 'type'. while read -r type; do echo -n $type: ## Fetch translation alternative from count-th entry echo "$input" | jshon -e 1 -e $count -e 1 -a -u | tr '\n' ',' | sed 's/,$/\n/' count=$(($count+1)) done</dev/null else ## Use sed to fetch first entry only. sed -e 's/\[\[\[[^[]*\[\[[^[]*\[\([^[]*\)\].*/\1/' -e 's/"//g' echo fi } _translate () { TEXT="$(tr '\n' ' ' | sed 's/[\t ]\+/%20/g')" ## If input is not a single word, disable alternative translations. We use a ## point in the grep string to ignore any space at the end. [ -n "$(echo $TEXT | grep '%20.')" ] && OPT_LONG=false 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 | _filter } if [ $# -ne 0 ]; then echo "$@" | _translate else _translate fi ## 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/'