-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate_selection
More file actions
executable file
·155 lines (132 loc) · 3.23 KB
/
translate_selection
File metadata and controls
executable file
·155 lines (132 loc) · 3.23 KB
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
#!/bin/bash
#
# Translate the selected text into the specified language.
#
# Dependencies: coreutils, gawk, jq, libnotify, sed, util-linux, wget, xsel
set -o nounset -o pipefail
readonly API_ENDPOINT="https://translate.googleapis.com/translate_a/single"
readonly USER_AGENT="Mozilla/5.0"
readonly L_OPTS="help,version"
readonly OPTS="hVv"
readonly CMD="${0##*/}"
readonly USAGE="${CMD} [OPTION]... LANG"
readonly USAGE_ERR_MSG=$(
cat <<END
Usage: ${USAGE}
Try '${CMD} --help' for more information.
END
)
readonly VERSION_INFO=$(
cat <<END
${CMD} (scripts) 1.0.0
This is free and unencumbered software released into the public domain.
For more information, please refer to <https://unlicense.org/>.
END
)
readonly HELP=$(
cat <<END
SYNOPSIS
${USAGE}
DESCRIPTION
Translate the selected text into LANG. Possible LANG values: en, de, etc.
See https://cloud.google.com/translate/docs/languages for a complete list.
OPTIONS
-h, --help
display this help and exit
-V, -v, --version
display version and exit
END
)
#######################################
# Encode special characters per RFC 3986
# Arguments:
# str - string to encode
#######################################
function urlencode() {
local -r str="$1"
local -r LC_ALL="C"
local char
while IFS="" read -r -d "" -n 1 char; do
case "${char}" in
[-_.~a-zA-Z0-9])
printf "%s" "${char}"
;;
*)
printf "%%%02x" "'${char}"
;;
esac
done < <(printf "%s" "${str}")
}
#######################################
# Main function
# Globals:
# API_ENDPOINT - Google Translate API endpoint
# CMD - script basename
# HELP - help message
# L_OPTS - long non-positional options (e.g. --help)
# OPTS - short non-positional options (e.g. --h)
# USAGE_ERR_MSG - usage error message
# USER_AGENT - HTTP User-Agent header
#######################################
function main() {
local opts
opts=$(
getopt \
--options=${OPTS} --longoptions=${L_OPTS} \
--name "${CMD}" \
-- "$@"
)
if (($? != 0)); then
echo "${USAGE_ERR_MSG}"
exit 2
fi
eval set -- "${opts}"
local need_help=false
local need_version=false
while true; do
case "$1" in
-h | --help)
need_help=true
shift
break
;;
-V | -v | --version)
need_version=true
shift
break
;;
--)
shift
break
;;
esac
done
if [[ $need_help == true ]]; then
echo "${HELP}"
exit 0
fi
if [[ $need_version == true ]]; then
echo "${VERSION_INFO}"
exit 0
fi
if (($# != 1)); then
echo "${CMD}: expected 1 argument; got $#"
echo "${USAGE_ERR_MSG}"
exit 2
fi
local -r selection=$(xsel --output)
local url="${API_ENDPOINT}?client=gtx&sl=auto&tl=$1&dt=t&q="
url+=$(urlencode "${selection}")
local translation
translation=$(
wget --output-document="-" --quiet --user-agent="${USER_AGENT}" "${url}" \
| jq --join-output ".[0] | .[] | .[0]" 2>/dev/null
)
if (($? != 0)); then
echo "${CMD}: unable to translate the selection '${selection}'"
echo "Try to check the selection validity and your internet connection"
exit 1
fi
notify-send --icon=info "${selection}" "${translation@E}"
}
main "$@"