from googletrans import Translator
def translate_text(text):
translator = Translator(service_urls=['translate.google.com'])
translation = translator.translate(text, src='pt', dest='en')
return translation.text
text_to_translate = "Olá, como você está?"
translated_text = translate_text(text_to_translate)
print(translated_text)
import requests
def translate_text(text):
subscription_key = 'your_subscription_key' # Replace with your subscription key
endpoint = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0'
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-type': 'application/json'}
params = {
'to': 'en',
'textType': 'plain',
}
body = [{'text': text}]
response = requests.post(endpoint, headers=headers, params=params, json=body)
translation = response.json()[0]['translations'][0]['text']
return translation
text_to_translate = "Olá, como você está?"
translated_text = translate_text(text_to_translate)
print(translated_text)
Использование DeepL API (Python):
import requests
def translate_text(text):
auth_key = 'your_auth_key' # Replace with your DeepL API authentication key
endpoint = 'https://api-free.deepl.com/v2/translate'
params = {
'auth_key': auth_key,
'text': text,
'source_lang': 'PT',
'target_lang': 'EN',
}
response = requests.post(endpoint, params=params)
translation = response.json()['translations'][0]['text']
return translation
text_to_translate = "Olá, como você está?"
translated_text = translate_text(text_to_translate)
print(translated_text)