Fix translation updater script: Handle nested modpacks, support games (#14340)

This commit is contained in:
Lars Müller 2024-02-05 20:57:30 +01:00 committed by GitHub
parent 83f779c52d
commit 4859cf44ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -166,16 +166,9 @@ def get_modname(folder):
if match: if match:
return match.group(1) return match.group(1)
except FileNotFoundError: except FileNotFoundError:
if not os.path.isfile(os.path.join(folder, "modpack.txt")): folder_name = os.path.basename(folder)
folder_name = os.path.basename(folder) # Special case when run in Minetest's builtin directory
# Special case when run in Minetest's builtin directory return "__builtin" if folder_name == "builtin" else folder_name
if folder_name == "builtin":
return "__builtin"
else:
return folder_name
else:
return None
return None
# If there are already .tr files in /locale, returns a list of their names # If there are already .tr files in /locale, returns a list of their names
def get_existing_tr_files(folder): def get_existing_tr_files(folder):
@ -463,27 +456,32 @@ def update_tr_file(dNew, mod_name, tr_file):
# Updates translation files for the mod in the given folder # Updates translation files for the mod in the given folder
def update_mod(folder): def update_mod(folder):
modname = get_modname(folder) if not os.path.exists(os.path.join(folder, "init.lua")):
if modname is not None: print(f"Mod folder {folder} is missing init.lua, aborting.")
print(f"Updating translations for {modname}")
data = 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))
else:
print(f"Unable to determine the mod name in folder {folder}. Missing 'name' field in mod.conf.", file=_stderr)
exit(1) exit(1)
assert not is_modpack(folder)
modname = get_modname(folder)
print(f"Updating translations for {modname}")
data = 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))
# Determines if the folder being pointed to is a mod or a mod pack def is_modpack(folder):
return os.path.exists(os.path.join(folder, "modpack.txt")) or os.path.exists(os.path.join(folder, "modpack.conf"))
def is_game(folder):
return os.path.exists(os.path.join(folder, "game.conf")) and os.path.exists(os.path.join(folder, "mods"))
# Determines if the folder being pointed to is a game, mod or a mod pack
# and then runs update_mod accordingly # and then runs update_mod accordingly
def update_folder(folder): def update_folder(folder):
is_modpack = os.path.exists(os.path.join(folder, "modpack.txt")) or os.path.exists(os.path.join(folder, "modpack.conf")) if is_game(folder):
if is_modpack: run_all_subfolders(os.path.join(folder, "mods"))
subfolders = [f.path for f in os.scandir(folder) if f.is_dir() and not f.name.startswith('.')] elif is_modpack(folder):
for subfolder in subfolders: run_all_subfolders(folder)
update_mod(subfolder)
else: else:
update_mod(folder) update_mod(folder)
print("Done.") print("Done.")