This tool for access translate.google.com from terminal and English dictionary.
main mirror GitHub - Anoncheg1/Command-line-translator: Command-line access to google translate and some other features
mirror [Bash] Google translate from command line and some more features - Pastebin.com
requirements: bash, cURL, SpiderMonkey, forvo.com account for audio
FEATURES:
- translated text, fixed text with highlight, language detection, dictionary, translit
for english: - phrases, ideom, transcription, audio pronunciation
- cache for words
- saving english words to file for learning
for convenience:
~/.bash_aliases
alias t="/home/user/translate"
alias ts="/home/user/translate -s"
old version:
#!/bin/bash
# This tool for access translate.google.com from terminal and to have English dictionary.
# mirror https://github.com/Anoncheg1/Command-line-translator
# mirror http://pastebin.com/kPTYjY6W
# requirements: curl, Mozilla js shell, forvo.com account
# + output: translated text, fixed text with highlight, dictionary, phrases, transcription, audio pronunciation
# + cache for all words
# + saving words to file for learning
trap bashtrap INT
bashtrap()
{
echo "CTRL+C Detected"
rm "$cachefile" 2> /dev/null
exit
}
help='translate [-s] <text>
if text is english, target language is ENGLISH_TARGET_LANG
otherwise, "target" language is english
-s, --sound Enable sound for one word'
# adjust to taste
ENGLISH_TARGET_LANG=ru #target language for english request
target=en #target for all languages except english
flogin=121212 #forvo.com login and pass REQUIRED!
fpass=121212
words_buffer=3000
PR_DIR="/home/$USER/.translate"
timeout=5
jsout_dictionary="var googlearr = eval(JSON.stringify(myJSONObject));
var rsum=\"\"; //translated text
if(typeof googlearr[0] !== 'undefined' && googlearr[0] !== null){ //summing sentences
for (var i = 0; i < googlearr[0].length; i++){
if(typeof googlearr[0][0] !== 'undefined' && googlearr[0][0] !== null){
rsum=rsum+googlearr[0][0];
}
}
}
print(\"\033[1;32m\"+rsum+\"\033[0m\");
/*arr=googlearr;
for (var c = 0; c < arr.length; c++){ //testing
if(typeof arr[c] !== 'undefined' && arr[c] !== null){ //dictionary output
for (var i = 0; i < arr[c].length; i++){
if(typeof arr[c] !== 'undefined' && arr[c] !== null){
for (var e = 0; e < arr[c].length; e++){
print(arr[c][e]);
}
}
}
}
}*/
function dictionary(arr){ //dictionary output
if(typeof arr[1] !== 'undefined' && arr[1] !== null){
for (var i = 0; i < arr[1].length; i++){
if(typeof arr[1][0] !== 'undefined' && arr[1][0] !== null){
print(\"\033[1;34m\"+arr[1][0]+\"\033[0m\");
for (var e = 0; e < arr[1][2].length; e++){
print(arr[1][2][e]);
}
}
}
}
}"
request=($*)
sound=0
if [[ $1 = -h || $1 = --help ]]; then
echo "$help"
exit
fi
if [[ $1 == -s || $1 == --sound ]];then
sound=1
request[0]=""
fi
request=${request
[*]}
source=auto
tchar=${request:0:1}
tcharnum=$(printf "%d" "'${tchar}")
if [[ $tcharnum -ge 65 && $tcharnum -le 122 ]]; then #if request is english
source=en
target="$ENGLISH_TARGET_LANG" #if text is english, "target" language is russian
fi
[ ! -d "$PR_DIR" ] && mkdir "$PR_DIR"
[ ! -d "$PR_DIR"/cache ] && mkdir "$PR_DIR"/cache
r_words_count=$(echo "$request" |wc -w)
if [[ ${#request} -gt 300 ]]; then
result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$request" http://translate.google.com)
encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result")
#iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8
echo -e "\033[32;1m"$(iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8)"\033[0m"
else
cachefile="$PR_DIR/cache/$request"
if [[ ! -e "$cachefile" || (( $sound == 1 && ! -e "$cachefile".mp3 )) ]]; then
grespond=$(curl -s -i --user-agent "" --data-urlencode "text=$request" "http://translate.google.com/translate_a/t?client=t&hl=$target&sl=$source&tl=$target&ie=UTF-8&oe=UTF-8&multires=1&ssel=0&tsel=0&sc=1") #getting google respond for short sentence
#echo -n "var myJSONObject = " > "$PR_DIR"/tmpjsobj2
#echo [$(echo "$grespond" | grep -o '"[^"]*/i[^"]*"')"];" >> "$PR_DIR"/tmpjsobj2
#echo [$(echo "$grespond" | grep -o '[^"]*/i[^"]*')"];"
#echo -e "var googlearr = eval(JSON.stringify(myJSONObject));\n print(googlearr);" >> "$PR_DIR"/tmpjsobj2
#js "$PR_DIR"/tmpjsobj2
if [[ $r_words_count -le 2 ]];then #getting google fixed text from $grespond
fl=$(echo "$grespond" | grep -o '[^"]*/i[^"]*' | sed 's/\\u003cb\\u003e\\u003ci\\u003e//g' | sed 's/\\u003c\/i\\u003e\\u003c\/b\\u003e//g' | sed 's/\\u0026//g' | sed "s/\#39;/'/g")
diffnum=$(cmp -l <(echo -n $request) <(echo -n $fl) 2>/dev/null | head -n 1 | sed 's/\([0-9][0-9]\?\).*/\1/')
# echo $diffnum
[[ $diffnum > 0 ]] && fl="$(tput bold)$(tput setaf 3)${fl:0:$diffnum-1}$(tput bold)$(tput setaf 1)${fl:$diffnum-1:1}$(tput bold)$(tput setaf 3)${fl:$diffnum}$(tput sgr0)" #highlight difference in one word
else
fl=$(echo "$grespond" | grep -o '[^"]*/i[^"]*' | sed 's/\\u003cb\\u003e\\u003ci\\u003e/'$(tput bold)$(tput setaf 3)'/g' | sed 's/\\u003c\/i\\u003e\\u003c\/b\\u003e/'$(tput sgr0)'/g' | sed 's/\\u0026//g' | sed "s/\#39;/'/g" ) #google fixed text
fi
# echo $grespond | grep -o '\[.*\]'
echo -n "var myJSONObject = " > "$PR_DIR"/tmpjsobj
echo -n "$grespond" | grep -o '\[.*\]' >> "$PR_DIR"/tmpjsobj
echo ";">> "$PR_DIR"/tmpjsobj
echo -n "$jsout_dictionary" >> "$PR_DIR"/tmpjsobj
[[ ! $fl ]] && echo -n "dictionary(googlearr);" >> "$PR_DIR"/tmpjsobj
#for english only. but it may be altered
if [[ $r_words_count -eq 1 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') != $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then #dictionary
js "$PR_DIR"/tmpjsobj > "$cachefile" #google translated text and dictionary to cache
raw_phras=$(curl -s --user-agent "" http://www.macmillandictionary.com/dictionary/american/"$(echo $request | tr ' ' - )" | grep -o '<li ID.*End of DIV SENSE--></li>' | sed 's/<.\?span[^>]*>//g' )
phras=$(echo $raw_phras | sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"h2">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2\./') #getting phrases
phras2=$(echo $raw_phras | sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"EXAMPLE">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2/') #getting examples
if [[ $phras ]]; then
echo -e "\033[1;35m"PHRASES:$(tput sgr0) >> "$cachefile"
join --nocheck-order -a 1 -a 2 <(echo "$phras") <(echo "$phras2") | sed '/^$/d' >> "$cachefile"
# echo "$phras" > tm1
# echo "$phras2" >tm2
# echo -e "\033[34m"http://www.macmillandictionary.com/dictionary/american/"$request"$(tput sgr0) >> "$cachefile"
fi
trans=$(curl -s --user-agent "" http://lingvopro.abbyyonline.com/en/Translate/en-ru/"$request" | grep -o '"[^"]*Handlers/TranscriptionHandler\.ashx[^"]*"' | sed 's/.*=\(.*\)"/\1/'| echo -n -e $(sed 's/+/ /g; s/%/\\x/g')) #getting transcription
[[ $trans ]] && echo "[$trans]" >> "$cachefile"
cat "$cachefile" 2>/dev/null #output
echo -e "\033[34m"http://oxforddictionaries.com/definition/english/"$request"$(tput sgr0) # just another good english dictionary
if [[ $trans ]] ; then
#saving words
if [[ ! $(grep "$request" "$PR_DIR"/translated_words 2>/dev/null) ]]; then
#echo -e "$request \t\t\t\t\t\t\t\t\t\t [$trans]" >> "$PR_DIR"/translated_words
echo -e "$request" >> "$PR_DIR"/translated_words
fi
#getting sound (always) from forvo.com
if [[ ! -e "$cachefile".mp3 ]]; then
curl -s -c "$PR_DIR"/tmpcookie --connect-timeout $timeout -m $timeout --user-agent "" -d "login=$flogin&password=$fpass" http://ru.forvo.com/login/ -o/dev/null
if [[ -e "$PR_DIR"/tmpcookie ]]; then
slink=$(curl -s -b $PR_DIR/tmpcookie --connect-timeout $timeout -m $timeout http://ru.forvo.com/word/"$request"/ | grep -o '[^"]*/download/mp3/'"$request"'/en/[^"]*' |head -n 1 )
[[ $slink ]] && curl -s -b "$PR_DIR"/tmpcookie --connect-timeout $timeout -m $timeout --user-agent "" -o "$cachefile".mp3 http://ru.forvo.com"$slink"
rm "$PR_DIR"/tmpcookie 2>/dev/null
else
echo fail to get sound from forvo.com
fi
fi
fi
[[ $(ls "$cachefile" | wc -l) -gt $words_buffer ]] && find "$PR_DIR"/cache -mtime +20 -delete #cache cleaning
else
js "$PR_DIR"/tmpjsobj #google dictionary and translated text output
[[ $fl ]] && echo $fl #google fixed text output
if [[ $r_words_count -gt 1 && $r_words_count -le 3 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') != $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then #phrases for 2 3 words of phrasal verbs
raw_phras=$(curl -s --user-agent "" http://www.macmillandictionary.com/dictionary/american/"$(echo $request | tr ' ' - )" | grep -o '<li.*End of DIV SENSE--></li>' | sed 's/<.\?span[^>]*>//g' )
phras=$(echo $raw_phras | sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"h2">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2\./') #getting phrases
phras2=$(echo $raw_phras | sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"EXAMPLE">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2/') #getting examples
if [[ $phras2 ]]; then
echo -e "\033[1;35m"PHRASES:$(tput sgr0)
join --nocheck-order -a 1 -a 2 <(echo "$phras") <(echo "$phras2") | sed '/^$/d'
fi
fi
fi
else #cache output
cat "$cachefile" #output
echo -e "\033[34m"http://oxforddictionaries.com/definition/english/"$request"$(tput sgr0)
fi
#sound output
if [[ $sound == 1 && -e "$cachefile".mp3 ]]; then
stat=$(playsound --loop 1 "$cachefile".mp3 2>&1 | grep -o 'Couldn')
[[ $stat ]] && rm "$cachefile".mp3
fi
fi
exit