Skip to content

Instantly share code, notes, and snippets.

@armanokka
Created June 4, 2022 20:50
Show Gist options
  • Save armanokka/bf686615f17ccca994bc16bd4aaf61a2 to your computer and use it in GitHub Desktop.
Save armanokka/bf686615f17ccca994bc16bd4aaf61a2 to your computer and use it in GitHub Desktop.
Translo API Python library
#[Main file]
import requests
class PyTranslate:
"Main class"
def __init__(self, token: str):
":token: token from Translo API (https://rapidapi.com/armangokka/api/translo)"
self.token = token
self.url = "https://translo.p.rapidapi.com/api/v3/"
def translate(self, translate_text: str, from_lang: str, to_lang: str, fast: bool = False):
"""
Translate function
Arguments:
:translate_text: text of translate
:from_lang: from the language of the text (CHECK LANG CODE FROM: rapidapi.com/armangokka/api/translo/details)
:to_lang: to language of the text (CHECK LANG CODE FROM: rapidapi.com/armangokka/api/translo/details)
:fast: fast mode translate
:return:
"""
payload = f"from={from_lang}&to={to_lang}&text={translate_text}&fast={str(fast).lower()}"
headers = {
"content-type": "application/x-www-form-urlencoded",
"X-RapidAPI-Host": "translo.p.rapidapi.com",
"X-RapidAPI-Key": self.token,
}
response = requests.request("POST", self.url + "translate", data=payload, headers=headers)
json = response.json()
try:
return json["translated_text"]
except:
raise Exception(json["message"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment