-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
39 lines (30 loc) · 1.29 KB
/
translator.py
File metadata and controls
39 lines (30 loc) · 1.29 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
import re
import time
from deep_translator import GoogleTranslator
def translate_line(line):
match = re.match(r"^(.*)=(.*)$", line)
if match:
prefix = match.group(1).strip()
text = match.group(2).strip()
if text:
try:
translated = GoogleTranslator(source='en', target='ru').translate(text)
print(f"Перевод: '{text}' -> '{translated}'")
return f"{prefix}={translated}"
except Exception as e:
print(f"Ошибка перевода строки: {line}. Причина: {e}")
return line
return line.strip()
input_file = "original version/en_us.lang"
output_file = "russian version/en_us.lang"
start_time = time.time()
with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
for line in infile:
if line.strip().startswith("#") or line.strip() == "":
outfile.write(line)
else:
translated_line = translate_line(line)
outfile.write(translated_line + "\n")
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Перевод завершен. Время выполнения: {elapsed_time:.2f} секунд.")