123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #!/bin/bash
- # Configuration
- # You need an API key. You can get it for free from https://www.deepl.com/pro-api
- key=""
- usage="usage: $0 [-h] [-l] target language [-t] text"
- lang="IT" # Target language (EN, FR, ES, RU...)
- # API request
- request () {
- response=$(curl -s https://api-free.deepl.com/v2/translate -d auth_key="$key" -d "text=$2" -d "target_lang=$1")
- echo $response
- }
- # Main
- while getopts ":hl:t:" option; do
- case $option in
- h)
- echo $usage;
- exit
- ;;
- l)
- lang=${OPTARG^^} # Uppercase
- ;;
- t)
- text=${OPTARG} # Text to translate
- ;;
- \?)
- echo "error: option -$OPTARG is not implemented";
- exit
- ;;
- esac
- done
- # Get stdin
- if [ -z "$text" ] && [ -p /dev/stdin ]; then
- text=$(cat /dev/stdin)
- fi
- if [ -z "$text" ]; then
- echo "error: text not provided or empty"
- echo $usage
- exit
- fi
- if [ -z "$key" ]; then
- echo "error: you need an API key. You can get it for free from https://www.deepl.com/pro-api"
- echo $usage
- exit
- fi
- response=$(request $lang "$text")
- # You can use jq, alternatively
- echo $response | cut -d: -f4 | cut -d'"' -f2
|