193 lines
6.7 KiB
Plaintext
193 lines
6.7 KiB
Plaintext
rpy python 3
|
|
|
|
init python in cli:
|
|
import os
|
|
import collections
|
|
|
|
def is_random_block(t):
|
|
return any("random:" in n.get_code() or "block:" in n.get_code() for n in t.block)
|
|
|
|
def write_translates(filename, language, filter):
|
|
generation = renpy.translation.generation
|
|
fn, common = generation.shorten_filename(filename)
|
|
|
|
# The common directory should not have dialogue in it.
|
|
if common:
|
|
return
|
|
|
|
tl_filename = os.path.join(renpy.config.gamedir, renpy.config.tl_directory, language, fn) # type: ignore
|
|
|
|
if tl_filename[-1] == "m":
|
|
tl_filename = tl_filename[:-1]
|
|
|
|
if language == "None":
|
|
language = None
|
|
|
|
translator = renpy.game.script.translator
|
|
|
|
# Truncate the file before rewrite
|
|
if os.path.exists(tl_filename):
|
|
with open(tl_filename, "w") as f:
|
|
f.truncate(0)
|
|
|
|
# Re-add everything with updated pointers and lines
|
|
for label, t in translator.file_translates[filename]:
|
|
|
|
# if (t.identifier, language) in translator.language_translates:
|
|
# continue
|
|
|
|
if hasattr(t, "alternate"):
|
|
if (t.alternate, language) in translator.language_translates:
|
|
continue
|
|
|
|
if generation.is_empty_extend(t):
|
|
continue
|
|
|
|
# if is_random_block(t):
|
|
# continue
|
|
|
|
if hasattr(t, "translatable") and t.translatable == False:
|
|
block = t.block
|
|
print(f"Found untranslatable node block: {block}")
|
|
continue
|
|
|
|
label = label or ""
|
|
|
|
with open(tl_filename, "a") as f:
|
|
f.write("# {}:{}\n".format(t.filename, t.linenumber))
|
|
f.write("translate {} {}:\n".format(language, t.identifier.replace('.', '_')))
|
|
f.write("\n")
|
|
|
|
# Grab original code block
|
|
# for n in t.block:
|
|
# f.write(" # " + n.get_code() + "\n")
|
|
|
|
translated = translator.language_translates.get((t.identifier, language)) or t
|
|
|
|
# Add commented code and an optional TODO if there's a mismatch.
|
|
for block1, block2 in zip(t.block, translated.block):
|
|
old = block1.get_code()
|
|
new = block2.get_code()
|
|
|
|
suffix = "\n" if old == new else " # TODO: Updated dialogue.\n" # TODO: This does not function, probably due to translated var being overridden.
|
|
f.write(" # " + old + suffix)
|
|
|
|
# Re-apply translation
|
|
for n in translated.block:
|
|
f.write(" " + n.get_code(filter) + "\n")
|
|
|
|
f.write("\n")
|
|
|
|
def append_unsanctioned_strings(language, seen):
|
|
"""Used to extract pre-existing strings, which can be a part of an unsanctioned translation. We want to keep those."""
|
|
|
|
generation = renpy.translation.generation
|
|
stl = renpy.game.script.translator.strings[language]
|
|
|
|
for old, new in stl.translations.items():
|
|
|
|
if not new or new == old:
|
|
continue
|
|
|
|
if old in seen: # TODO: Unsanctioned translations sometimes double for some reason despite this check
|
|
continue
|
|
|
|
if old not in stl.translation_loc:
|
|
continue
|
|
|
|
filename, linenumber = stl.translation_loc[old]
|
|
tlfn = os.path.join(renpy.config.basedir, filename)
|
|
|
|
with open(tlfn, "a+") as f:
|
|
f.seek(0)
|
|
contents = f.read()
|
|
prefix = u"translate {} strings:\n".format(language)
|
|
|
|
if prefix not in contents:
|
|
f.write(prefix)
|
|
f.write(u"\n")
|
|
|
|
# f.write(u" # {}:{}\n".format(generation.elide_filename(filename), linenumber)) # TODO: This gives incorrect results; Seek alternative methods.
|
|
f.write(u" # Unsanctioned translation\n")
|
|
f.write(u" old \"{}\"\n".format(generation.quote_unicode(old)))
|
|
f.write(u" new \"{}\"\n".format(generation.quote_unicode(new)))
|
|
f.write(u"\n")
|
|
|
|
def write_strings(language, filter, min_priority, max_priority, common_only, only_strings=[]): # @ReservedAssignment
|
|
generation = renpy.translation.generation
|
|
|
|
if language == "None":
|
|
stl = renpy.game.script.translator.strings[None] # @UndefinedVariable
|
|
else:
|
|
stl = renpy.game.script.translator.strings[language] # @UndefinedVariable
|
|
|
|
strings = renpy.translation.scanstrings.scan(min_priority, max_priority, common_only)
|
|
stringfiles = collections.defaultdict(list)
|
|
|
|
for s in strings:
|
|
|
|
tlfn = generation.translation_filename(s)
|
|
|
|
if tlfn is None:
|
|
continue
|
|
|
|
# Already seen.
|
|
if s.text in stl.translations:
|
|
continue
|
|
|
|
if language == "None" and tlfn == "common.rpy":
|
|
tlfn = "common.rpym"
|
|
|
|
if only_strings and s.text not in only_strings:
|
|
continue
|
|
|
|
stringfiles[tlfn].append(s)
|
|
|
|
seen = set()
|
|
|
|
for tlfn, sl in stringfiles.items():
|
|
|
|
# sl.sort(key=lambda s : (s.filename, s.line))
|
|
|
|
tlfn = os.path.join(renpy.config.gamedir, renpy.config.tl_directory, language, tlfn)
|
|
|
|
with open(tlfn, "a") as f:
|
|
|
|
f.write(u"translate {} strings:\n".format(language))
|
|
f.write(u"\n")
|
|
|
|
for s in sl:
|
|
seen.add(s.text)
|
|
text = filter(s.text)
|
|
|
|
f.write(u" # {}:{}\n".format(generation.elide_filename(s.filename), s.line))
|
|
f.write(u" old \"{}\"\n".format(generation.quote_unicode(s.text)))
|
|
f.write(u" new \"{}\"\n".format(generation.quote_unicode(text)))
|
|
f.write(u"\n")
|
|
|
|
# Re-add unsanctioned translation strings
|
|
append_unsanctioned_strings(language, seen)
|
|
|
|
def retranslate():
|
|
translator = renpy.game.script.translator
|
|
generation = renpy.translation.generation
|
|
filter = generation.null_filter
|
|
|
|
scripts = generation.translate_list_files()
|
|
nscripts = len(scripts)-1
|
|
nlanguages = len(translator.languages)
|
|
|
|
for i, language in enumerate(translator.languages):
|
|
|
|
for j, filename in enumerate(scripts):
|
|
n = round(float(j)/nscripts*100)
|
|
print("\rGenerating translation for {} {}% ({}/{})...".format(language, n, i+1, nlanguages), end="")
|
|
|
|
write_translates(filename, language, filter)
|
|
|
|
write_strings(language, filter, 0, 299, False)
|
|
|
|
return False
|
|
|
|
renpy.arguments.register_command("retranslate", retranslate)
|