🐍 The Python Code

from urllib.parse import quote_plus as urlEncode
import requests

def translate(targetLanguage, translateString):
    sentences = []
    for sentence in requests.get(f"https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=" + urlEncode(targetLanguage) + "&q=" + urlEncode(translateString)).json()['sentences']:
        sentences.append(sentence['trans'])
    return "".join(sentences)

def fallback(targetLanguage, translateString):
    sentences = []
    for sentence in requests.get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl=" + urlEncode(targetLanguage) + "&q=" + urlEncode(translateString)).json()[0]:
        sentences.append(sentence[0])
    return "".join(sentences)

🚧 What It Does

This simple piece of code translates any text using a free, unlimited, and hidden google translate API.

👨🏾‍💻 Implementation In Other Languages

This code can be implemented in any language, as it just queries a URL, with the necessary encoded information, and receives a response. I have intentionally used long variable/function names, so that anyone can just read the code to translate it.

📓 How I Built It

After about an hour of very annoying network request inspecting, on devtools, I found two endpoints that could work. One returns more information than the other, so I created a translate and fallback method. The response, from either one, has all the sentences separated, so I just create an empty list, add each sentence to it, and return a string with all the elements, or sentences, joined together.

For a list of google translate language codes, click here.
Share this project:

Updates