1
0
Fork 0

Add translate function

This commit is contained in:
tastytea 2022-05-01 10:50:26 +02:00
parent 00fa0a0b1f
commit 06fb99154d
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#compdef translate
_arguments '-h[short help text]' \
'--help[short help text]' \
'--domain[domain of instance to use]:domain:_ssh_hosts' \
'--from[Language to translate from]:language:_translate_languages' \
'--to[Language to translate to]:language:_translate_languages' \
'*:text:'
function _translate_languages()
{
local -a languages=($(curl -s https://libretranslate.de/languages \
| jq --raw-output '.[].code'))
_values "language" ${languages}
}
# Local Variables:
# mode: shell-script
# End:

31
.config/zsh/functions/translate Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env zsh
# Translate text with LibreTranslate
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
zmodload zsh/zutil
local -a o_help=()
local -a o_domain=()
local -a o_from=()
local -a o_to=()
zparseopts -D -K -- h=o_help -help=o_help -domain:=o_domain \
-from:=o_from -to:=o_to
if [[ ${#o_help} -ne 0 ]]; then
print "usage: ${0} [-h|--help] [--domain <DOMAIN>]" \
"[--from <LANG>] [--to <LANG>] TEXT …"
print "If no TEXT is supplied, input will be taken from STDIN"
return
fi
local domain="libretranslate.de"
local from="en"
local to="de"
[[ ${#o_domain} -eq 2 ]] && domain=${o_domain[2]}
[[ ${#o_from} -eq 2 ]] && from=${o_from[2]}
[[ ${#o_to} -eq 2 ]] && to=${o_to[2]}
local text="${@}"
[[ ! -v 1 ]] && text=$(cat -)
curl --silent -X POST \
--header 'Content-Type: application/json' \
--data '{"q": "'${text}'", "source": "'${from}'", "target": "'${to}'", "format": "text"}' \
https://${domain}/translate | jq --raw-output .translatedText