-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtranslate
More file actions
executable file
·59 lines (50 loc) · 2.11 KB
/
xtranslate
File metadata and controls
executable file
·59 lines (50 loc) · 2.11 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
#!/usr/bin/env bash
#
# ############################################################################
# Project: scripts (none)
# File...: xtranslate
# Created: Saturday, 2023/01/14 - 13:08:04
# Author.: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Thursday, 2025/02/20 - 02:47:50
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 1.0.1.145
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# > script to translate text
# ############################################################################
# HISTORY:
#
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~
# shellcheck disable=SC1090,SC2154
source "$xSHELL_INIT"
use args colors
xrequirements curl jq xclip-cmd
_translate(){
local text="$@"
# Make the translation using curl
output=$(
curl -s -A "Mozilla/5.0" -G \
--data-urlencode "client=gtx" \
--data-urlencode "sl=auto" \
--data-urlencode "tl=$lang" \
--data-urlencode "dt=t" \
--data-urlencode "q=$text" \
"http://translate.googleapis.com/translate_a/single"
)
# extract translation from json output
translation="$(echo "$output" | jq -r '.[0][0][0]')"
}
_to_clipboard(){ echo "$translation" | xclip-cmd > /dev/null; }
xarg --id -t,--text --desc "set <text> to translate" --var text+r
xarg --id -T,--text-clipboard --desc "fill <text> from clipboard"
xarg --id -c,--clipboard --desc "save translation to clipboard"
xarg --id -l,--language --desc "define <lang> to translate" --var lang+r
xarg --header "translations using 'Google Translate'"
xrun --xrequire-one --xreject-unknown "$@"
[ -z "$lang" ] && lang=en
xcase t "_translate $text"
xcase T "_translate $(xclip-cmd -p)"
xcase c "_to_clipboard"
xsys.write "$translation"