-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.sh
executable file
·67 lines (59 loc) · 1.54 KB
/
translate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# access translate.google.com from terminal
# credits: [email protected]/forums
# see: http://crunchbang.org/forums/viewtopic.php?pid=176917#p176917
# adjust to taste
DEFAULT_TARGET_LANG=en
help () {
echo 'translate <text> [[<source language>] <target language>]'
echo 'if target missing, use DEFAULT_TARGET_LANG'
echo 'if source missing, use auto'
}
querry () {
# $1 -> text to translate
# $2 -> source language (can be auto)
# $3 -> target language
curl \
--silent \
--include \
--user-agent '' \
-d "sl=$2" \
-d "tl=$3" \
--data-urlencode "text=$1" \
http://translate.google.com
}
find_encoding () {
awk \
'/Content-Type: .* charset=/ {
sub(/^.*charset=["'\'']?/,"");
sub(/[ "'\''].*$/,"");
print
}'
}
extrct_result () {
# $1 -> the encoding of the input
iconv -f $1 | \
awk \
'BEGIN {RS="</div>"};
/<span[^>]* id=["'\'']?result_box["'\'']?/' | \
html2text -utf8
}
if [[ $1 = -h || $1 = --help ]]; then
help
exit
fi
if [[ $3 ]]; then
source="$2"
target="$3"
elif [[ $2 ]]; then
source=auto
target="$2"
else
source=auto
target="$DEFAULT_TARGET_LANG"
fi
result=$(querry "$1" "$source" "$target")
encoding=$(find_encoding <<<"$result")
#iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8
echo "$result" | extrct_result $encoding
exit