Last active
July 30, 2023 10:21
-
-
Save goura/dffd14106005f3ce4754c045b60c8450 to your computer and use it in GitHub Desktop.
A simple command line interface to the DeepL API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A simple command line interface to the DeepL API. | |
# Author: Kazuhiro Ogura | |
# License: MIT | |
if ! command -v jq &> /dev/null; then | |
echo "You need jq to run this script. Try 'sudo apt install jq' on Ubuntu or 'brew install jq' on macOS." | |
exit | |
fi | |
api_key="$DEEPL_AUTH_KEY" | |
if [ -z "$api_key" ]; then | |
echo "The DEEPL_AUTH_KEY environment variable isn't set. Set it to your DeepL API key." | |
exit | |
fi | |
DEEPL_SERVER_URL=${DEEPL_SERVER_URL:-"https://api.deepl.com/v2"} | |
api_url="${DEEPL_SERVER_URL}/translate" | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
BLUE='\033[1;34m' | |
MAGENTA='\033[1;35m' | |
NC='\033[0m' # No Color | |
function validate_lang() { | |
local lang=$(echo "$1" | tr '[:upper:]' '[:lower:]') | |
local valid_langs=("bg" "cs" "da" "de" "el" "en" "en-gb" "en-us" "es" "et" "fi" "fr" "hu" "id" "it" "ja" "ko" "lt" "lv" "nb" "nl" "pl" "pt" "pt-br" "pt-pt" "ro" "ru" "sk" "sl" "sv" "tr" "uk" "zh") | |
for valid_lang in "${valid_langs[@]}"; do | |
if [[ "$valid_lang" == "$lang" ]]; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
while true; do | |
echo "Enter 'q' to quit." | |
echo -ne "${BLUE}Language from? ([enter] to autodetect): ${NC}" | |
read lang_from | |
if [ "$lang_from" = "q" ]; then | |
exit | |
fi | |
if [ -n "$lang_from" ] && ! validate_lang "$lang_from"; then | |
echo "Not a valid language code: $lang_from" | |
continue | |
fi | |
echo -ne "${BLUE}Language to?: ${NC}" | |
read lang_to | |
if [ "$lang_to" = "q" ]; then | |
exit | |
fi | |
if ! validate_lang "$lang_to"; then | |
echo "Not a valid language code: $lang_to" | |
continue | |
fi | |
echo -ne "${BLUE}Text?: ${NC}" | |
read text | |
if [ "$text" = "q" ]; then | |
exit | |
fi | |
echo -ne "${GREEN}Calling the backend...${NC}\r" | |
response=$(curl -s -X POST "$api_url" \ | |
-H "Authorization: DeepL-Auth-Key $api_key" \ | |
--data-urlencode "text=$text" \ | |
-d "source_lang=$lang_from" \ | |
-d "target_lang=$lang_to") | |
echo -ne '\033[0K\r' | |
translated_text=$(echo "$response" | jq -r '.translations[0].text') | |
echo -e "${MAGENTA}️ ✨ ✨ ✨ Translation ✨ ✨ ✨ ${NC}" | |
echo -e "${RED}$translated_text${NC}" | |
if [ -n "$lang_from" ] && [ -n "$lang_to" ]; then | |
echo -ne "${BLUE}translate back? (y/N): ${NC}" | |
read translate_back | |
if [ "$translate_back" = "y" ] || [ "$translate_back" = "yes" ]; then | |
echo -ne "${GREEN}Calling the backend...${NC}\r" | |
response=$(curl -s -X POST "$api_url" \ | |
-H "Authorization: DeepL-Auth-Key $api_key" \ | |
--data-urlencode "text=$translated_text" \ | |
-d "source_lang=$lang_to" \ | |
-d "target_lang=$lang_from") | |
echo -ne '\033[0K\r' | |
back_translated_text=$(echo "$response" | jq -r '.translations[0].text') | |
echo -e "${RED}Back translation: $back_translated_text${NC}" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment