forked from lorenzovalentijn/AndroidStringsTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.py
More file actions
60 lines (39 loc) · 1.46 KB
/
Copy pathtranslate.py
File metadata and controls
60 lines (39 loc) · 1.46 KB
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
import re
import uuid
import time
from deep_translator import GoogleTranslator
source_lang_code = 'en'
target_lang_codes = ['nl', 'fr', 'de', 'pl', 'es']
regex = '<(“[^”]*”|\'[^’]*’|[^\'”>])*>'
with open('source') as source:
body = source.read()
def translate():
result = open("result_" + time.strftime("%c") + ".txt", "w")
print(source_lang_code)
print(body)
result.write("\n" + source_lang_code + "\n")
result.write(body + "\n\n")
pattern = re.compile(regex)
htmlTags = pattern.finditer(body)
for target_lang_code in target_lang_codes:
translator = GoogleTranslator(source=source_lang_code, target=target_lang_code)
replaceDictionary = dict()
index = 0
for tag in htmlTags:
if tag.group() not in replaceDictionary.values():
index += 1
replaceDictionary[uuid.uuid4().hex[:6].upper()] = tag.group()
newBody = body
# remove html
for key, value in replaceDictionary.items():
newBody = newBody.replace(value, key)
newTranslation = translator.translate(newBody)
# re-add html
for key, value in replaceDictionary.items():
newTranslation = newTranslation.replace(key, value)
translated_body = newTranslation
print(target_lang_code)
print(translated_body)
result.write(target_lang_code + "\n")
result.write(translated_body + "\n\n")
translate()