File size: 1,258 Bytes
f6ae060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b4e8f4
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import requests

def translate_text(text, source_lang, target_lang):
    url = "https://google-translator9.p.rapidapi.com/v2"
    payload = {
        "q": text,
        "source": source_lang,
        "target": target_lang,
        "format": "text"
    }
    headers = {
        "x-rapidapi-key": os.getenv("RAPIDAPI_KEY"),
        "x-rapidapi-host": "google-translator9.p.rapidapi.com",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        print('=====================', response.json())
        translations = response.json().get('data', {}).get('translations', [])
        if translations:
            translated_text = translations[0].get('translatedText', '')
            return translated_text
        else:
            return text
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return text

if __name__ == "__main__":
    text_to_translate = "Dans cette image donne moi l'œdème"
    source_language = "fr"
    target_language = "en"
    translation = translate_text(text_to_translate, source_language, target_language)
    if translation:
        print(translation)