From e26d40f23e2beb13a7c7d70a380cea7a5fa80ce2 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 26 Nov 2024 15:12:44 +0100 Subject: [PATCH] Locale updater script now updates PO files as well --- DEV_TRANSLATION_WORKFLOW.md | 4 +- ...ale_templates.py => update_locale_files.py | 63 ++++++++++++++++--- 2 files changed, 55 insertions(+), 12 deletions(-) rename update_locale_templates.py => update_locale_files.py (60%) diff --git a/DEV_TRANSLATION_WORKFLOW.md b/DEV_TRANSLATION_WORKFLOW.md index d7058700..ff95b0bc 100644 --- a/DEV_TRANSLATION_WORKFLOW.md +++ b/DEV_TRANSLATION_WORKFLOW.md @@ -24,8 +24,8 @@ You need: ## Part 1: Pushing the translations from the game to Weblate: 1. Clean up: Make sure the game repository is in a clean state (no non-committed changes) -2. Update POT files: Run `update_locale_templates.py` in the root directory of this repositoryand commit the changes (if any) -3. Update PO files: TODO +2. Update POT files: Run `update_locale_files.py pot` in the root directory of this repository and commit the changes (if any) +3. Update PO files: Run `update_locale_files.py po` in the root directory of this repository and commit the changes (if any) 4. Push: Push the changes to the online repository of the game 5. Update Weblate repository (optional): Weblate should soon automatically update its repository. But if you want to want the new strings to be available immediately, go to the project page, then “Manage > Repository Maintenance” and click “Update” diff --git a/update_locale_templates.py b/update_locale_files.py similarity index 60% rename from update_locale_templates.py rename to update_locale_files.py index 1bc96871..314c28ca 100755 --- a/update_locale_templates.py +++ b/update_locale_files.py @@ -3,11 +3,16 @@ ########################################################################## ##### ABOUT THIS SCRIPT ################################################## -# This script updates the translation template files (*.pot) of all mods -# by running the xgettext application. +# This script updates the translation template files (*.pot) or +# translation files (*.po) of all mods by using the gettext tools. # It requires you have the 'gettext' software installed on your system. # -# Run this script, and the *.pot files will be updated. +# INVOCATION: +# ./update_locale_files.py [mode] +# +# where "mode" is either "pot" if you want to update the *.pot files +# or "po" if you want to update the *.po files. If "mode" is omitted, +# it defaults to "pot". ########################################################################## @@ -19,11 +24,34 @@ PACKAGE_NAME = "Repixture" import os import re +import sys # pattern for the 'name' in mod.conf pattern_name = re.compile(r'^name[ ]*=[ ]*([^ \n]*)') # file name pattern for gettext translation template files (*.pot) pattern_pot = re.compile(r'(.*)\.pot$') +# file name pattern for gettext translation files (*.po) +pattern_po = re.compile(r'(.*)\.po$') + +def invoke_msgmerge(template_file, mod_folder, modname): + containing_path = os.path.dirname(template_file) + + po_files = [] + for root, dirs, files in os.walk(os.path.join(mod_folder)): + for f in files: + match = pattern_po.match(f) + if match: + po_files.append(f) + print(po_files) + if len(po_files) > 0: + for po_file in po_files: + po_file = os.path.join("locale", po_file) + po_file = os.path.join(mod_folder, po_file) + command = "msgmerge --backup=none -U '"+po_file+"' '"+template_file+"'" + return_value = os.system(command) + if return_value != 0: + print("ERROR: msgmerge invocation returned with "+str(return_value)) + exit(1) def invoke_xgettext(template_file, mod_folder, modname): containing_path = os.path.dirname(template_file) @@ -43,14 +71,20 @@ def invoke_xgettext(template_file, mod_folder, modname): print("ERROR: xgettext invocation returned with "+str(return_value)) exit(1) -def update_locale_template(folder, modname): +def update_locale_template(folder, modname, mode): for root, dirs, files in os.walk(os.path.join(folder, 'locale')): for name in files: code_match = pattern_pot.match(name) - if code_match == None: + if code_match == None: continue fname = os.path.join(root, name) - invoke_xgettext(fname, folder, modname) + if mode == "pot": + invoke_xgettext(fname, folder, modname) + elif mode == "po": + invoke_msgmerge(fname, folder, modname) + else: + print("ERROR: invalid locale template mode!") + exit(1) def get_modname(folder): try: @@ -67,21 +101,30 @@ def get_modname(folder): return None return None -def update_mod(folder): +def update_mod(folder, mode): modname = get_modname(folder) if modname != None: print("Updating '"+modname+"' ...") - update_locale_template(folder, modname) + update_locale_template(folder, modname, mode) def main(): + mode = "" + if len(sys.argv) >= 2: + mode = sys.argv[1] + if mode == "": + mode = "pot" + if mode != "po" and mode != "pot": + print("ERROR: invalid mode specified. Provide either 'po', 'pot' or nothing as command line argument") + exit(1) + for modfolder in [f.path for f in os.scandir("./mods") if f.is_dir() and not f.name.startswith('.')]: is_modpack = os.path.exists(os.path.join(modfolder, "modpack.txt")) or os.path.exists(os.path.join(modfolder, "modpack.conf")) if is_modpack: subfolders = [f.path for f in os.scandir(modfolder) if f.is_dir() and not f.name.startswith('.')] for subfolder in subfolders: - update_mod(subfolder) + update_mod(subfolder, mode) else: - update_mod(modfolder) + update_mod(modfolder, mode) print("All done.") main()