Skip to content

Instantly share code, notes, and snippets.

@ayubmalik
Last active July 14, 2024 06:18
Show Gist options
  • Save ayubmalik/149e2c7f28104f61cc1c862fe9834793 to your computer and use it in GitHub Desktop.
Save ayubmalik/149e2c7f28104f61cc1c862fe9834793 to your computer and use it in GitHub Desktop.
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
#!/bin/bash
# uncomment and fix with appropriate values if you are behind a proxy
#export https_proxy='http://localhost:3128'
sl=en
tl=$(basename $0)
if [[ "${tl}" != "es" ]]; then
sl=es
fi
base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q="
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' )
full_url=${base_url}${qry}
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
@digitalsushi
Copy link

digitalsushi commented Jan 3, 2021

I'm on a mac and I know sed is a little different over here. I'm not the sed guru but this works for me on MacOS 10.15.2. To anyone looking at this, my sed command, which works on a Mac, is more naively turning each literal space into a plus sign, which urlencodes the query. This is only fractionally complete and will not cover other whitespace, and multiples of whitespace. It's a Sunday afternoon of vacation here and I am pleased with this minimal effort.

I added a third requirement, of having the jq JSON utility installed.

#!/bin/bash
sl=en
tl=$(basename $0)
if [[ "${tl}" != "es" ]]; then
  sl=es
fi
base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q="
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/\ /+/g" )
full_url=${base_url}${qry}
response=$(curl -sA "${ua}" "${full_url}")
echo ""
#print only first translation from JSON
echo ${response} | jq -r '.[0][0][0]'

@digitalsushi
Copy link

Actually one final pass for today, I have this now in my .bash_profile and take the destination language as the first parameter.

cat .bash_profile
function gtr {
  sl=en
  tl=$1
  shift
  base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q="
  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/\ /+/g" )
  full_url=${base_url}${qry}
  response=$(curl -sA "${ua}" "${full_url}")
  echo ${response} | jq -r '.[0][0][0]'
}

Example output.

$ gtr es i would love a jelly donut
me encantaría una rosquilla de gelatina
$ gtr de i would love a jelly donut
Ich würde einen Gelee-Donut lieben

@ayubmalik
Copy link
Author

Hi, yes I think using jq is much better, I use it in my day job. I flip between macos and Linux and I think the default bash shell in macos and hence sed is a slightly older version.

@digitalsushi
Copy link

Yes, agreed - thank you very much ayubmalik! I should have said so in my first reply, it's a wonderful addition to the community.

@greksak
Copy link

greksak commented Nov 2, 2022

Hi. Thanks guys - nice script.
I had a problem with encoding special characters when using source language like slovak, czech, polish... Only change i did was in query string. Instead of using sed (replace space with +) I used jq for complete url encoding. This is my query that works correctly for all special chars:
qry=$( echo $@ | jq -sRr @uri )

@ayubmalik
Copy link
Author

Nice will update the gist in a bit. Thanks.

@rsanjuan87
Copy link

Less dependencies

function gtr {
  sl=en
  tl=$1
  shift
  base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q="
  ua='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0'
  qry=$( echo $@ | sed -e "s/\ /+/g" )
  full_url=${base_url}${qry}
  response=$(curl -sA "${ua}" "${full_url}")
  echo ${response}  | sed 's/","/\n/g' | sed -E 's/\[|\]|"//g' | head -1
}

@kkew3
Copy link

kkew3 commented May 20, 2023

Exploit curl more:

gtr() {
  sl=en
  tl=$1
  shift
  qry="$@"
  ua='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0'
  base_url='https://translate.googleapis.com/translate_a/single'

  resp="$(curl \
    --silent \
    --get \
    --user-agent "$ua" \
    --data client=gtx \
    --data sl="$sl" \
    --data tl="$tl" \
    --data dt=t \
    --data-urlencode q="$qry" \
    "$base_url")"
  echo "$resp" |
      sed 's/","/\n/g' |
      sed -E 's/\[|\]|"//g' |
      head -1
}

I didn't try out this verbose version of command, but did try on curl -sGA "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0" -d client=gtx -d sl=auto -d tl=en -d dt=t --data-urlencode q="hello world" "https://translate.googleapis.com/translate_a/single" and it worked.

@alexdbsas
Copy link

function gtr {
  sl='auto'
  tl=$1
  shift
  base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=$>
  ua='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Fi>
  qry=$(printf "%s" "$@" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')
  full_url=${base_url}${qry}
  response=$(curl -sA "${ua}" "${full_url}")
  echo "${response}" | sed 's/","/\n/g' | sed -E 's/\[|\]|"//g' | head -1
}

for non-english locales

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment