-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepl
More file actions
executable file
·65 lines (53 loc) · 1.64 KB
/
deepl
File metadata and controls
executable file
·65 lines (53 loc) · 1.64 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
#!/bin/bash
# https://www.deepl.com/docs-api/translate-text/translate-text/
# https://github.com/DeepLcom/openapi/blob/main/openapi.yaml
# https://www.deepl.com/docs-api/api-access/openapi/
# https://www.deepl.com/pro-api/
if [ -z "$DEEPL_API_KEY" ]; then
echo 'Please set $DEEPL_API_KEY to one from your account. It used to be under https://www.deepl.com/account/summary .
e.g. with:
$ export DEEPL_API_KEY="12434...."
'
exit 1
fi
if [ $# -lt 2 -o $# -gt 3 ]; then
echo '# Usage:
deepl SRC_LANG DST_LANG [TEXT OR STDIN]
# Examples:
deepl EN DE "This is test."
echo "This is test." | deepl EN DE
cat README.md | deepl EN DE
# For using API Pro endpoint
export DEEPL_API_PRO=pro
# Fur using custom endpoint
export DEEPL_API_ENDPOINT="http...."
'
exit 1
fi
srcl="$1"
dstl="$2"
if [ $# -eq 3 ]; then
text="$3" # for --data-urlencode
else
text="$(cat)" # for --data-urlencode
fi
if [ -z "$DEEPL_API_ENDPOINT" ]; then
if [ -z "$DEEPL_API_PRO" ]; then
DEEPL_API_ENDPOINT='https://api-free.deepl.com/v2/translate'
else
DEEPL_API_ENDPOINT='https://api.deepl.com/v2/translate'
fi
fi
# set -x #for debugging, also you may want to consider to comment "#| jq.."
response=$(curl --silent -X POST "${DEEPL_API_ENDPOINT}" \
-H "Authorization: DeepL-Auth-Key ${DEEPL_API_KEY}" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "text=${text}" \
-d "source_lang=${srcl}" \
-d "target_lang=${dstl}")
if echo "$response" | jq -e '.translations' > /dev/null; then
echo "$response" | jq -r '.translations[0].text'
else
echo "Error: $(echo "$response" | jq -r '.message')"
exit 1
fi