Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created August 16, 2023 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/3ad52274023bcf1696d57a5d565c27b6 to your computer and use it in GitHub Desktop.
Save ehzawad/3ad52274023bcf1696d57a5d565c27b6 to your computer and use it in GitHub Desktop.
import httpx
import asyncio
from collections import OrderedDict
# async def translate_to_arabic(text):
# url = "https://google-translate1.p.rapidapi.com/language/translate/v2"
# payload = {
# "q": text,
# "target": "ar",
# "source": "en"
# }
# headers = {
# "content-type": "application/x-www-form-urlencoded",
# "Accept-Encoding": "application/gzip",
# "X-RapidAPI-Key": "8b76055345msh8869fbe14ed4641p11bdb2jsn31510f6627b0",
# "X-RapidAPI-Host": "google-translate1.p.rapidapi.com"
# }
# async with httpx.AsyncClient() as client:
# response = await client.post(url, data=payload, headers=headers)
# return response.json()['data']['translations'][0]['translatedText']
#
async def translate_to_arabic(text):
url = "https://google-translate1.p.rapidapi.com/language/translate/v2"
payload = {
"q": text,
"target": "ar",
"source": "en"
}
headers = {
"content-type": "application/x-www-form-urlencoded",
"Accept-Encoding": "application/gzip",
"X-RapidAPI-Key": "8b76055345msh8869fbe14ed4641p11bdb2jsn31510f6627b0",
"X-RapidAPI-Host": "google-translate1.p.rapidapi.com"
}
async with httpx.AsyncClient() as client:
response = await client.post(url, data=payload, headers=headers)
try:
return response.json()['data']['translations'][0]['translatedText']
except KeyError:
print(f"Error translating text: {text}")
print("Response:", response.json())
return text # Return the original text if translation fails
async def main():
file_path = '/home/ehz/Downloads/language.txt'
translated_file_path = 'translated.txt'
# Read the content of the file
with open(file_path, 'r') as file:
content = file.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment