-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
66 lines (42 loc) · 1.59 KB
/
translate.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
import random
import re
from pathlib import Path
import bs4
def translator(string: str) -> str:
string = string.rstrip('.')
for pattern in ('%1$@', '%1$s'):
pos = string.find(pattern)
if pos != -1:
pos += len(pattern)
string = string[:pos] + ')' + string[pos:]
if random.randint(1, 43) == 42:
return string + '...)'
return string + ')'
def translate_xml(text: str) -> str:
soup = bs4.BeautifulSoup(text, 'xml')
for s in soup.find_all('string'):
s.string = translator(s.string)
return str(soup)
def translate_strings(text: str) -> str:
def repl(match):
return match.group(1) + translator(match.group(2)) + match.group(3)
result, _ = re.subn(r'^(".+" = ")(.+)(";)$', repl, text, flags=re.MULTILINE)
return result
def translate(path: Path = Path('translations')):
path_original = path / 'original'
path_dest = path / 'skobochka'
print(f'Starting translation in path: {path_original}\n')
for f_original in path_original.iterdir():
print(f' Processing: {f_original}')
f_dest = path_dest / f_original.name.replace('_ru_', '_skobochka_')
translation = f_original.read_text(encoding='utf-8')
result = translation
if f_original.suffix == '.strings':
result = translate_strings(translation)
if f_original.suffix == '.xml':
result = translate_xml(translation)
print(f'Saving result: {f_dest}\n')
f_dest.write_text(result, encoding='utf-8')
print('Finished!')
if __name__ == '__main__':
translate()