Skip to content

Instantly share code, notes, and snippets.

@farbod-s
Last active October 1, 2021 13:48
Show Gist options
  • Save farbod-s/1de25ecf37dd629f79c621617f144e1b to your computer and use it in GitHub Desktop.
Save farbod-s/1de25ecf37dd629f79c621617f144e1b to your computer and use it in GitHub Desktop.
#!/bin/bash
#----------
# Set variables
sl='en'
tl=''
query=''
#----------
# Get the options
while getopts ":s:t:q:" option; do
case $option in
s) # source language
sl=$OPTARG;;
t) # translate language
tl=$OPTARG;;
q) # query text
query=$OPTARG;;
\?) # Invalid option
echo "Error: Invalid option"
exit;;
esac
done
#----------
query=$( echo $query | sed -e "s/\ /+/g" )
url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${query}"
agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
response=$(curl -sA "${agent}" "${url}")
translate=$( echo ${response} | sed 's/","/\n/g' | sed -E 's/\[|\]|"//g' | head -1 )
echo "$translate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment