-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
61 lines (44 loc) · 1.56 KB
/
translator.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
from google.cloud import translate
import os
def translate_text(english_path: str, text):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'keys.json'
project_id = os.getenv('PROJECT_ID')
client = translate.TranslationServiceClient()
location = "global"
parent = f"projects/{project_id}/locations/{location}"
response = client.translate_text(
request={
"parent": parent,
"contents": [text],
"mime_type": "text/plain",
"source_language_code": "kn",
"target_language_code": "en-US",
}
)
for translation in response.translations:
f = open(english_path, "a", encoding="utf-8")
lines = translation.translated_text.split("\n")
string_without_empty_lines = ""
for line in lines:
string_without_empty_lines += line + "\n"
f.write(string_without_empty_lines)
f.close()
def translate_locale_out(locale_path: str, english_path: str):
tamil_text = ""
with open(locale_path, 'r', encoding="utf8") as file:
line = file.readline()
cnt = 1
while line:
tamil_text = tamil_text + line
cnt += 1
line = file.readline()
l = len(tamil_text)
while l > 0:
if l > 30000:
translate_text(english_path, tamil_text[:30000])
tamil_text = tamil_text[30000:]
l = len(tamil_text)
else:
translate_text(english_path, tamil_text)
l = 0
# translate_locale_out("localOutput.txt", "englishOutPut.txt")