From 815b5cb0867c2011be7031b74331b99de94326e4 Mon Sep 17 00:00:00 2001 From: Muhammad Rifqi Priyo Susanto Date: Sun, 28 Apr 2024 05:12:30 +0700 Subject: [PATCH] Translation updater: Add comment propagation (#14416) --- util/README_mod_translation_updater.md | 3 +++ util/mod_translation_updater.py | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/util/README_mod_translation_updater.md b/util/README_mod_translation_updater.md index fa4304ac8..f128bbe0c 100644 --- a/util/README_mod_translation_updater.md +++ b/util/README_mod_translation_updater.md @@ -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 diff --git a/util/mod_translation_updater.py b/util/mod_translation_updater.py index 51c870b75..8d537841a 100755 --- a/util/mod_translation_updater.py +++ b/util/mod_translation_updater.py @@ -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"))