Translation updater: Add comment propagation (#14416)

This commit is contained in:
Muhammad Rifqi Priyo Susanto 2024-04-28 05:12:30 +07:00 committed by GitHub
parent 0837d674eb
commit 815b5cb086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 6 deletions

@ -174,6 +174,9 @@ files, associating them with the line that follows them. So for example:
# are also supported
Text as well=
The script will also propagate comments from an existing `template.txt` to all `*.tr`
files and write it above existing comments (if exist).
There are also a couple of special comments that this script gives special treatment to.
#### Source file comments

@ -194,13 +194,16 @@ def mkdir_p(path):
# dKeyStrings is a dictionary of localized string to source file sets
# dOld is a dictionary of existing translations and comments from
# the previous version of this text
def strings_to_text(dkeyStrings, dOld, mod_name, header_comments, textdomain):
def strings_to_text(dkeyStrings, dOld, mod_name, header_comments, textdomain, templ = None):
# if textdomain is specified, insert it at the top
if textdomain != None:
lOut = [textdomain] # argument is full textdomain line
# otherwise, use mod name as textdomain automatically
else:
lOut = [f"# textdomain: {mod_name}"]
if templ is not None and templ[2] and (header_comments is None or not header_comments.startswith(templ[2])):
# header comments in the template file
lOut.append(templ[2])
if header_comments is not None:
lOut.append(header_comments)
@ -225,8 +228,14 @@ def strings_to_text(dkeyStrings, dOld, mod_name, header_comments, textdomain):
val = dOld.get(localizedString, {})
translation = val.get("translation", "")
comment = val.get("comment")
templ_comment = None
if templ:
templ_val = templ[0].get(localizedString, {})
templ_comment = templ_val.get("comment")
if params["break-long-lines"] and len(localizedString) > doublespace_threshold and not lOut[-1] == "":
lOut.append("")
if templ_comment != None and templ_comment != "" and (comment is None or comment == "" or not comment.startswith(templ_comment)):
lOut.append(templ_comment)
if comment != None and comment != "" and not comment.startswith("# textdomain:"):
lOut.append(comment)
lOut.append(f"{localizedString}={translation}")
@ -431,13 +440,14 @@ def generate_template(folder, mod_name):
templ_file = os.path.join(folder, "locale/template.txt")
write_template(templ_file, dOut, mod_name)
return dOut
new_template = import_tr_file(templ_file) # re-import to get all new data
return (dOut, new_template)
# Updates an existing .tr file, copying the old one to a ".old" file
# if any changes have happened
# dNew is the data used to generate the template, it has all the
# currently-existing localized strings
def update_tr_file(dNew, mod_name, tr_file):
def update_tr_file(dNew, templ, mod_name, tr_file):
if params["verbose"]:
print(f"updating {tr_file}")
@ -445,7 +455,7 @@ def update_tr_file(dNew, mod_name, tr_file):
dOld = tr_import[0]
textOld = tr_import[1]
textNew = strings_to_text(dNew, dOld, mod_name, tr_import[2], tr_import[3])
textNew = strings_to_text(dNew, dOld, mod_name, tr_import[2], tr_import[3], templ)
if textOld and textOld != textNew:
print(f"{tr_file} has changed.")
@ -463,12 +473,12 @@ def update_mod(folder):
assert not is_modpack(folder)
modname = get_modname(folder)
print(f"Updating translations for {modname}")
data = generate_template(folder, modname)
(data, templ) = generate_template(folder, modname)
if data == None:
print(f"No translatable strings found in {modname}")
else:
for tr_file in get_existing_tr_files(folder):
update_tr_file(data, modname, os.path.join(folder, "locale/", tr_file))
update_tr_file(data, templ, modname, os.path.join(folder, "locale/", tr_file))
def is_modpack(folder):
return os.path.exists(os.path.join(folder, "modpack.txt")) or os.path.exists(os.path.join(folder, "modpack.conf"))