#Expects scripts files inside the scripts folder. Takes strings from the trans.csv file, and applies it to the scripts file. import csv import os # def parse_string(input_string): # import re # # Use a regular expression to match the substrings between the quotes # matches = re.findall(r'\"(.+?)\"', input_string) # # Split the input string on the quotes # split_string = re.split(r'\"', input_string) # # The first element in the split string is the leading whitespace, so we can remove it # split_string = split_string[1:] # # Zip the split string and the matches together, then unpack them into two separate lists # split_list, matches_list = zip(*zip(split_string, matches)) # # Concatenate the lists and return the result # return list(split_list) + list(matches_list) # Create the output folder if it doesn't exist if not os.path.exists('output'): os.makedirs('output') if not os.path.exists('scripts'): os.makedirs('scripts') # Open the CSV file with the translations with open('trans.csv', 'r', encoding='utf8') as translations_file: translations_reader = csv.reader(translations_file) # Create a dictionary to store the translations translations = {} # Iterate through the rows of the CSV file for row in translations_reader: # The first column is the English string and the second column is the Spanish translation english_string = row[0] spanish_translation = row[1] translations[english_string] = spanish_translation # Iterate through all the .rpy files in the folder for file_name in os.listdir('scripts'): if file_name.endswith('.rpy'): # Open the .rpy file with open(os.path.join('scripts', file_name), 'r', encoding='utf8') as rpy_file: lines = rpy_file.readlines() # Open the output file for writing with open(os.path.join('output', file_name), 'w', encoding='utf8') as output_file: # Iterate through the lines of the .rpy file for line in lines: # Check if the line is commented out if not line.strip().startswith('#'): words = line.split('"')[1:-1] # Replace the English string with the translated string try: words = "".join(words) line = line.replace(words,translations[words]) except: pass # Write the modified line to the output file output_file.write(line)