@@ -43,16 +43,31 @@ def translate_text(text, target_language):
43
43
print (f"Error during translation: { e } " )
44
44
return None
45
45
46
- def translate_file (file_path , target_language ):
46
+ def translate_file (file_path , target_language , lines_to_translate = - 1 ):
47
47
translated_lines = []
48
48
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 ) :
50
50
if '=' in line and '###' in line :
51
51
text_to_translate = line .split ('=' )[1 ].split ('###' )[0 ].strip ()
52
+ print ('Sending: ' + text_to_translate + ' and translating to ' + target_language + '.' )
52
53
translated_text = translate_text (text_to_translate , target_language )
54
+ print ('Received: ' + translated_text )
53
55
translated_lines .append (line .replace (text_to_translate , translated_text ))
54
56
else :
55
57
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