Skip to content

Commit 8b35f19

Browse files
committed
updated langtrans
1 parent 967084d commit 8b35f19

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

LangFileTranslator/langtrans_old.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,31 @@ def translate_text(text, target_language):
4343
print(f"Error during translation: {e}")
4444
return None
4545

46-
def translate_file(file_path, target_language):
46+
def translate_file(file_path, target_language, lines_to_translate=-1):
4747
translated_lines = []
4848
with open(file_path, 'r', encoding='utf-8') as file:
49-
for line in file:
49+
for line_number, line in enumerate(file, start=1):
5050
if '=' in line and '###' in line:
5151
text_to_translate = line.split('=')[1].split('###')[0].strip()
52+
print('Sending: ' + text_to_translate + ' and translating to ' + target_language + '.')
5253
translated_text = translate_text(text_to_translate, target_language)
54+
print('Received: ' + translated_text)
5355
translated_lines.append(line.replace(text_to_translate, translated_text))
5456
else:
5557
translated_lines.append(line)
56-
return translated_lines
57-
58-
print(translate_text("Hello, how are you?", "Spanish"))
58+
59+
# Break the loop if the specified number of lines has been translated
60+
if 0 < lines_to_translate == line_number:
61+
break
62+
63+
# Write the translated lines to a new file
64+
output_file_path = file_path.replace(".lang", "_translated.lang")
65+
with open(output_file_path, 'w', encoding='utf-8') as file:
66+
for line in translated_lines:
67+
file.write(line)
68+
69+
print("Translation complete. File saved as: " + output_file_path)
70+
71+
# Example usage
72+
translate_file('C:\\Users\\juedwards\\Downloads\\en_US_goo_game.lang', 'Spanish', 20) # Translate first 20 lines
73+
# translate_file('C:\\Users\\juedwards\\Downloads\\en_US_goo_game.lang', 'Spanish') # Translate the whole file

0 commit comments

Comments
 (0)