-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranslate.sh
executable file
·232 lines (215 loc) · 6.65 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /bin/bash
# Console Google Translate
# License: MIT
# Repository URL: https://github.com/OPHoperHPO/GT-bash-client
# Updated by Anodev https://github.com/OPHoperHPO/
# Example usage: translate.sh <source_lng> <new_lng> <your text>
# This bash script is combine of this scripts:
# https://gist.github.com/elFua/3342075
# https://github.com/dominictarr/JSON.sh (License MIT)
# https://gist.github.com/ayubmalik/149e2c7f28104f61cc1c862fe9834793
# The JSON.sh script has been slightly modified for tight integration with my code.
USAGE="
Console Google Translate!
License: MIT
Repository url: https://github.com/OPHoperHPO/GT-bash-client
Example usage: '$0 <source_lng> <new_lng> <your text>'
Usage: '$0 en ru text'
Some language codes: en|fr|de|ru|nl|it|es|ja|la|pl|bo|ru|auto
All language codes: https://cloud.google.com/translate/docs/languages"
# Check curl installation
if [[ ! $(curl -V) ]]
then
echo -e "ERROR!"
echo -e "To run this script, you need install 'curl'"
echo -e "To install curl run this command:
'sudo pacman -S curl' # For Arch Linux based distro
'sudo dnf install curl' # For Fedora/Red Hat Enterprise Linux etc
'sudo apt install curl' #For Debian-based distro like Ubuntu, Debian, etc "
exit 1
fi
# Print USAGE if parse wrong arguments
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
# Code from JSON.sh https://github.com/dominictarr/JSON.sh
throw() {
echo "$*" >&2
exit 1
}
BRIEF=0
LEAFONLY=0
PRUNE=0
NO_HEAD=0
NORMALIZE_SOLIDUS=0
awk_egrep () {
local pattern_string=$1
gawk '{
while ($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern="$pattern_string"
}
tokenize () {
local GREP
local ESCAPE
local CHAR
if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1
then
GREP='egrep -ao --color=never'
else
GREP='egrep -ao'
fi
if echo "test string" | egrep -o "test" >/dev/null 2>&1
then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]'
else
GREP=awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]'
fi
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
# Force zsh to expand $A into multiple words
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
}
parse_array () {
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
parse_value "$1" "$index"
index=$((index+1))
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
:
}
parse_object () {
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
:
}
function has_substring() {
[[ "$1" != "${2/$1/}" ]]
}
parse_value () {
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in
'{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
isleaf=1
[ "$value" = '""' ] && isempty=1
;;
esac
[ "$value" = '' ] && return
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
:
# Fixed issue #5 Multi-string text input https://github.com/OPHoperHPO/GT-bash-client/issues/5
if echo "[$jpath]" | grep -q '\[0,[0-9]*,0\]'; then # We are only looking for translated text
value=${value:1:-1} # Delete double quotes on each line
value=${value//'\n'} # Delete "\n" at the end of each line if a line with the number of lines >1 was input
printf "%s\n" "$value"
fi
}
parse () {
read -r token
parse_value
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
}
# The code from JSON.sh is over
# Parse arguments to vars
FROM_LNG=$1
TO_LNG=$2
# Fixed text encoding issue. Issue URL: https://github.com/OPHoperHPO/GT-bash-client/issues/1
urlencode() {
# Code was copyied from https://gist.github.com/zhangkaiyulw/8793cd8641d270f1461a
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 |
while read x;
do printf "%%%s" "$(echo "$x" | tr "[:lower:]" "[:upper:]" )";
done
esac
done
}
shift 2
# Parse arguments to vars
qry=$( urlencode "$*" ) # Get query from arguments and encode it to URL encoding
# Define utility variables
ua='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' # User Agent
url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${FROM_LNG}&tl=${TO_LNG}&dt=t&q=${qry}" # Google Translate url
# Send encoded text and language to google translate api
response=$(curl --tr-encoding -sA "${ua}" "${url}")
# We execute the request, parse the json response, display all the translated text
translated=$(echo "${response}" | tokenize | parse )
echo "$translated"