Auto translator

This commit is contained in:
GManon 2022-12-23 22:20:58 -03:00
parent e59ecf694a
commit 3bcd319d78
9 changed files with 216 additions and 1 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.rpy
*.rpyc
*.bak
*.bak
*.txt
*.csv

62
Auto Trans/Applier.py Normal file
View File

@ -0,0 +1,62 @@
#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('#'):
# Iterate through the translations
for english_string, spanish_translation in translations.items():
# Replace the English string with the translated string
line = line.replace(english_string, spanish_translation)
# Write the modified line to the output file
output_file.write(line)

View File

18
Auto Trans/Parser.py Normal file
View File

@ -0,0 +1,18 @@
#Expects Renpy's dialogue.txt, spits out a original.csv file
import csv
# Open the text file for reading
with open('dialogue.txt', 'r', encoding='utf8') as input_file:
# Open the csv file for writing, using utf8 encoding
with open('original.csv', 'w', newline='', encoding='utf8') as output_file:
# Create a csv writer object
writer = csv.writer(output_file)
# Iterate over the lines in the text file
for line in input_file:
# Strip the newline characters from the line
stripped_line = line.strip()
# Write the original string and an empty string to the csv file
writer.writerow([stripped_line, ''])

29
Auto Trans/README.md Normal file
View File

@ -0,0 +1,29 @@
This was mostly made using the GPL chatbot, enjoy!
It requires googletrans and colorama (For le pretty colors)
`pip install googletrans==3.1.0a0`
and
`pip install colorama`
It expects a dialogue.txt generated from renpy. I personally only include dialogue (That is, no screen text) but it should work regardless if you choose it.
Including the tags is NECESSARY, otherwise the applier.py will fail.
They are meant to be run in this order
(Place your scripts to translate in the scripts folder)
(Place dialogue.txt on the same folder as the python script)
Parser.py
Translator.py
Applier.py
And your translated scripts should be in the output folder.
Doing this to snootgame took around an hour to autotranslate, googletrans also doesn't use the official Google API, so don't tell em!
#This is not a general use tool, you WILL need to modify it to adjust to your needs, and due the nature of the googletrans library it might break any moment
#I wanted to do this the legit way but the jews at Deppl will NOT give out free tier API keys to south americans (smh, fucking racists).
If you need help or something just open up an issue ig.
Also, Ñ.

78
Auto Trans/Translator.py Normal file
View File

@ -0,0 +1,78 @@
#Translates the game using good ol fashioned google translator. Be sure to replace the dialogue code in the translator and the tags in es_tags
#Also this breaks a tiny bit if the dialogue contains \n, but oh well. I don't want to open that can of worms.
#Also by break I mean that it may leave some text in english, is not that it will implode... I think
import csv
import os
from colorama import init
from colorama import Fore
from googletrans import Translator
from datetime import datetime, timedelta
translator = Translator()
# For Colorama to work
init()
times = []
# Good ol google doesn't know what renpy tags are, so it translates them. This was the cheapest solution.
es_tags = {
"{rápido}":"{fast}",
"{w =": "{w=",
"{alfa":"{alpha"
}
def avrg(nums):
average = sum(nums)/len(nums)
return timedelta(seconds=average)
# Open the input and output CSV files with the utf-8 encoding
with open('original.csv', 'r', encoding='utf-8') as input_file, \
open('trans.csv', 'w', encoding='utf-8',newline='') as output_file:
length = len(input_file.readlines())
input_file.seek(0)
# Create readers and writers for the input and output files
reader = csv.reader(input_file)
writer = csv.writer(output_file)
# Loop through the rows in the input file
for i, row in enumerate(reader):
start_time = datetime.now().replace(microsecond=0)
try:
# Translate the string
translation = translator.translate(row[0], dest='es')
for tag_es, tag in es_tags.items():
translation.text = translation.text.replace(tag_es,tag)
#Hope this works for escaping lol
translation.text = translation.text.replace('"',r'\"')
except Exception as e:
print(f'{Fore.RESET}An error occurred: {e}')
# So we know where we left off (Not yet implemented :fangcute:)
writer.writerow([row[0], 'aezakmi'])
break
else:
current_time = datetime.now().replace(microsecond=0)
# Calculate the time taken to translate the current string
time_taken = current_time - start_time
times.append(time_taken.total_seconds())
# Calculate the estimated time remaining to translate the whole file
time_remaining = avrg(times) * (length - i)
print(f'{Fore.RED}Original: {Fore.RESET}{row[0]} | {Fore.GREEN}Translation: {Fore.RESET}{translation.text}{" "*10}\n{Fore.CYAN}Estimated time remaining: {str(time_remaining).split(".")[0]}', end="\r")
writer.writerow([row[0], translation.text])
print(f"\n\n{Fore.YELLOW}FINISHED")

26
Auto Trans/differ.py Normal file
View File

@ -0,0 +1,26 @@
#This diffs two csv files, It's useful for mods and stuff.
#... I haven't actually tried this yet, it might work, it might not.
# If it doesn't. Call my number (It's hidden in your ear!).
import csv
def compare_csv_files(file1, file2):
# read the files line by line and store them in sets
with open(file1, 'r') as f1:
lines1 = set(f1.readlines())
with open(file2, 'r') as f2:
lines2 = set(f2.readlines())
# find the lines that are present only in one of the files
only_in_file1 = lines1 - lines2
only_in_file2 = lines2 - lines1
# write the lines to a new file
with open('comparison_result.csv', 'w') as result_file:
writer = csv.writer(result_file)
writer.writerows(only_in_file1)
writer.writerows(only_in_file2)
compare_csv_files('file1.csv', 'file2.csv')

View File