Last active
April 3, 2020 11:06
-
-
Save Dabsunter/57afdaefb52336b19531a964c355eacc to your computer and use it in GitHub Desktop.
Un petit utilitaire pour assister dans la traduction des fichiers .str des jeux Battle for the Earth (1/2/rotwk)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Apr 3 12:47:04 2020 | |
@author: davni | |
""" | |
from googletrans import Translator | |
import re | |
translator = Translator() | |
# go sur regex101.com pour comprendre le truc en dessous x) | |
regex = r"(?P<tag>\w+:.+)\n(?://(?P<comment>.+)\n)*\"(?P<text>.*)\"\nEND" | |
print(" === LOTR.str translator (mono file version) ===") | |
print("Yop, bon alors pour commencer on va choisir la langue d'arrivée (en,fr,de,...)") | |
lang_to = input("Langue d'arrivée: ") | |
print("Bon, à ce stade tu dois avoir le fichier {}.str dans le dossier où tu as lancé ce script.".format(lang_to)) | |
print("Si ce n'est pas encore le cas il est encore temps de la faire !") | |
input("C'est bon ? (Entrer)") | |
print() | |
print(" --- Début de la traduction ---") | |
print() | |
from_file = open('{}.str'.format(lang_to), 'r', encoding="cp1252") | |
new_file = open('{}_new.str'.format(lang_to), 'a', encoding="cp1252") | |
new_file.write("// --- Generated by LOTR.str translator ---") | |
matches = re.finditer(regex, from_file.read(), re.MULTILINE) | |
from_file.close() | |
for m in matches: | |
tag = m.group(1) | |
print("tag:", tag) | |
comment = m.group(2) | |
if comment: | |
print("comment:", comment) | |
text = m.group(3) | |
print("text:", text) | |
t = translator.translate(text, lang_to) | |
if t.src == lang_to: | |
print("Déjà traduit") | |
guess = text | |
else: | |
print("langue détectée:", t.src) | |
print("Proposition de Google Traduction:") | |
guess = t.text | |
print("->", guess) | |
if t.src != lang_to: | |
print("Votre traduction: (Entrer pour valider proposition)") | |
user = input() | |
if user: | |
guess = user | |
new_file.write("\n") | |
new_file.write(tag + "\n") | |
if comment: | |
new_file.write("//" + comment + "\n") | |
new_file.write('"' + guess + '"\n') | |
new_file.write("END\n") | |
new_file.flush() | |
print() | |
new_file.close() | |
print("Game Over") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Apr 2 22:12:19 2020 | |
@author: Dabsunter | |
""" | |
from googletrans import Translator | |
import re | |
translator = Translator() | |
# go sur regex101.com pour comprendre le truc en dessous x) | |
regex = r"(?P<tag>\w+:.+)\n(?://(?P<comment>.+)\n)*\"(?P<text>.*)\"\nEND" | |
print(" === LOTR.str translator ===") | |
print("Yop, bon alors pour commencer on va choisir les langues de départ/arrivé (en,fr,de,...)") | |
lang_from = input("Langue de départ: ") | |
lang_to = input("Langue d'arrivée: ") | |
print("Bon, à ce stade tu dois avoir le fichier {}.str dans le dossier où tu as lancé ce script.".format(lang_from)) | |
print("Si ce n'est pas encore le cas il est encore temps de la faire !") | |
input("C'est bon ? (Entrer)") | |
old_trans = dict() | |
check_old = False | |
if input("Y a-t-il in fichier {}.str duquel on peut déjà tirer les traductions ? (yes/no)".format(lang_to)) == "yes": | |
input("Pareil, il faut qu'il soit à côté de {}.str ! (Entrer)".format(lang_from)) | |
print() | |
print("Lecture des traductions existantes...") | |
old_file = open('{}.str'.format(lang_to), 'r', encoding="cp1252") | |
matches = re.finditer(regex, old_file.read(), re.MULTILINE) | |
old_file.close() | |
for m in matches: | |
old_trans[m.group(1)] = m.group(3) | |
print("Fait.") | |
if input("Veux-tu tout de même vérifier ces traductions ? (yes/no)") == "yes": | |
check_old = True | |
print() | |
print(" --- Début de la traduction ---") | |
print() | |
from_file = open('{}.str'.format(lang_from), 'r', encoding="cp1252") | |
new_file = open('{}_new.str'.format(lang_to), 'a', encoding="cp1252") | |
new_file.write("// --- Generated by LOTR.str translator ---") | |
matches = re.finditer(regex, from_file.read(), re.MULTILINE) | |
from_file.close() | |
for m in matches: | |
tag = m.group(1) | |
print("tag:", tag) | |
comment = m.group(2) | |
if comment: | |
print("comment:", comment) | |
text = m.group(3) | |
print("text:", text) | |
is_old = tag in old_trans | |
if is_old: | |
guess = old_trans[tag] | |
print("Traduction trouvée dans l'ancienne version:") | |
else: | |
guess = translator.translate(text, lang_to, lang_from).text | |
print("Proposition de Google Traduction:") | |
print("->", guess) | |
if not is_old or check_old: | |
print("Votre traduction: (Entrer pour valider la proposition)") | |
user = input() | |
if user: | |
guess = user | |
new_file.write("\n") | |
new_file.write(tag + "\n") | |
if comment: | |
new_file.write("//" + comment + "\n") | |
new_file.write('"' + guess + '"\n') | |
new_file.write("END\n") | |
new_file.flush() | |
print() | |
new_file.close() | |
print("Game Over") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment