Skip to content

Instantly share code, notes, and snippets.

@kkew3
Last active August 11, 2023 08:58
Show Gist options
  • Save kkew3/42a2c7caa575ba2502607d9396bf762f to your computer and use it in GitHub Desktop.
Save kkew3/42a2c7caa575ba2502607d9396bf762f to your computer and use it in GitHub Desktop.
Python script that invokes Google translation, depending on `requests` library.
import json
import sys
import requests
tl, query = sys.argv[1:]
sl = 'auto'
ua = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) '
'Gecko/20100101 Firefox/109.0')
query = query.strip()
# json response from Google transaltion server
jr = requests.get(
'https://translate.googleapis.com/translate_a/single',
params={
'client': 'gtx',
'sl': sl,
'tl': tl,
'dt': 't',
'q': query,
},
headers={
'user-agent': ua,
}).json()
# the translation result
tr = ''.join(x[0] for x in jr[0])
print(tr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment