-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibreTranslate.py
69 lines (61 loc) · 2.32 KB
/
libreTranslate.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
from alerts.alert_handler import alert
import traceback
def translate_text(text: str,
orig_lang: str = "auto",
trans_lang: str = "pt"):
url = "http://localhost:5000/translate"
dataJSON = {
'q': text.encode('utf-8'),
'source': orig_lang,
'target': trans_lang
}
print(f"Traduzindo de '{orig_lang}' para '{trans_lang}'...")
try:
response = requests.post(url, data=dataJSON)
except Exception as e:
traceback.print_exc()
alert("(!) Erro ao traduzir a legenda de '" + orig_lang + "' para '" + trans_lang + "': \n" + str(e))
# Verifica se a requisição foi bem-sucedida
if response.status_code == 200:
if 'translatedText' in response.json():
return response.json()['translatedText']
else:
print(f"Response não contém 'translatedText'. Revise o formato do JSON: {response.json()}")
else:
alert(f"Requisição de tradução falhou com o código de status {response.status_code}. Resposta: {response.text}")
def returnLanguageCode(language_name: str):
translation_languages = [
{'code':'ar','name':'Arabic'},
{'code':'az','name':'Azerbaijani'},
{'code':'zh','name':'Chinese'},
{'code':'cs','name':'Czech'},
{'code':'da','name':'Danish'},
{'code':'nl','name':'Dutch'},
{'code':'en','name':'English'},
{'code':'fi','name':'Finnish'},
{'code':'fr','name':'French'},
{'code':'de','name':'German'},
{'code':'el','name':'Greek'},
{'code':'he','name':'Hebrew'},
{'code':'hi','name':'Hindi'},
{'code':'hu','name':'Hungarian'},
{'code':'id','name':'Indonesian'},
{'code':'ga','name':'Irish'},
{'code':'it','name':'Italian'},
{'code':'ja','name':'Japanese'},
{'code':'ko','name':'Korean'},
{'code':'fa','name':'Persian'},
{'code':'pl','name':'Polish'},
{'code':'pt','name':'Portuguese'},
{'code':'ru','name':'Russian'},
{'code':'sk','name':'Slovak'},
{'code':'es','name':'Spanish'},
{'code':'sv','name':'Swedish'},
{'code':'tr','name':'Turkish'},
{'code':'uk','name':'Ukranian'},
{'code':'pt','name':'Portuguese'}
]
for language in translation_languages:
if language['name'].lower() == language_name.lower():
return language['code']