deepl-cli 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. # Configuration
  3. # You need an API key. You can get it for free from https://www.deepl.com/pro-api
  4. key=""
  5. usage="usage: $0 [-h] [-l] target language [-t] text"
  6. lang="IT" # Target language (EN, FR, ES, RU...)
  7. # API request
  8. request () {
  9. response=$(curl -s https://api-free.deepl.com/v2/translate -d auth_key="$key" -d "text=$2" -d "target_lang=$1")
  10. echo $response
  11. }
  12. # Main
  13. while getopts ":hl:t:" option; do
  14. case $option in
  15. h)
  16. echo $usage;
  17. exit
  18. ;;
  19. l)
  20. lang=${OPTARG^^} # Uppercase
  21. ;;
  22. t)
  23. text=${OPTARG} # Text to translate
  24. ;;
  25. \?)
  26. echo "error: option -$OPTARG is not implemented";
  27. exit
  28. ;;
  29. esac
  30. done
  31. # Get stdin
  32. if [ -z "$text" ] && [ -p /dev/stdin ]; then
  33. text=$(cat /dev/stdin)
  34. fi
  35. if [ -z "$text" ]; then
  36. echo "error: text not provided or empty"
  37. echo $usage
  38. exit
  39. fi
  40. if [ -z "$key" ]; then
  41. echo "error: you need an API key. You can get it for free from https://www.deepl.com/pro-api"
  42. echo $usage
  43. exit
  44. fi
  45. response=$(request $lang "$text")
  46. # You can use jq, alternatively
  47. echo $response | cut -d: -f4 | cut -d'"' -f2