forked from ils94/LOTROChatTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_chat.py
46 lines (31 loc) · 1.39 KB
/
common_chat.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
from googletrans import Translator
def return_text(file_path, start_line=None):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
if start_line is not None:
lines = lines[start_line:]
last_lines = lines[-6:]
messages = []
for line in last_lines:
if '[To' in line or 'You have joined the chat room for your Fellowship, Fellowship chat is now available.' in line or 'has come online.' in line or 'has gone offline.' in line:
continue
parts = line.split('] ', 1)
if len(parts) > 1:
timestamp = parts[0].strip('[] ')
player_message = parts[1].strip().strip("'")
messages.append((timestamp, player_message))
return messages
def translate_messages(lang, messages, processed_timestamps):
playername = ''
text = ''
translator = Translator(service_urls=['translate.googleapis.com'])
new_translations = []
for timestamp, message in messages:
if timestamp not in processed_timestamps:
if ': ' in message:
playername, text = message.split(': ', 1)
x = translator.translate(text, dest=lang)
translated_message = f"[{timestamp}] {playername}: {x.text}'"
processed_timestamps.add(timestamp)
new_translations.append(translated_message)
return new_translations