Skip to content

Instantly share code, notes, and snippets.

Forked from ayubmalik/es
Created Apr 14, 2021
What would you like to do?
Bash script using Google Translate API to translate English to Spanish using curl and sed only. You can change the 'sl' and 'tl' (source/target language) query parameter to whatever you want. Optionally if you create a symlink and call it 'en' it will translate back to spanish
# uncomment and fix with appropriate values if you are behind a proxy
#export https_proxy='http://localhost:3128'
tl=$(basename $0)
if [[ "${tl}" != "es" ]]; then
ua='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
qry=$( echo $@ | sed -E 's/\s{1,}/\+/g' )
response=$(curl -sA "${ua}" "${full_url}")
echo ""
#print only first translation from JSON
echo ${response} | sed 's/","/\n/g' | sed -E 's/\[|\]|"//g' | head -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment