#!/usr/bin/env python3 # # generate_progress_tool_localizations.py # # The Installer Progress helper is a minimal tool that minimizes loading resolve resources. # This script reads the localizations from Sparkle/*.lproj/Sparkle.strings # and generates a header with the handful of strings the tool needs. # It also keeps CFBundleLocalizations in InstallerProgress-Info.plist in sync # with the set of locales found. # # This script must be re-run by hand after changing any of the strings below in # Sparkle/*.lproj/Sparkle.strings, or after adding/removing a locale. # Output is deterministic between runs. import glob import json import os import plistlib import subprocess import sys SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) REPO_ROOT = os.path.dirname(SCRIPT_DIR) SPARKLE_DIR = os.path.join(REPO_ROOT, "Sparkle") OUTPUT_HEADER = os.path.join(REPO_ROOT, "Sparkle", "InstallerProgress", "SPUInstallerProgressLocalizedStrings.h") INFO_PLIST = os.path.join(REPO_ROOT, "Sparkle", "InstallerProgress", "InstallerProgress-Info.plist") # Fixed order so regenerating the header without content changes never reorders keys REQUIRED_KEYS = ( "Cancel Update", "Installing update…", "Updating %@", ) # Base.lproj holds the canonical/development-language (English) strings, but "Base" # is not a real locale identifier for +[NSBundle preferredLocalizationsFromArray:] # to match against, so it's keyed as "en" in the generated table. LPROJ_LOCALE_OVERRIDES = { "Base": "en", } def load_strings(strings_path): result = subprocess.run( ["plutil", "-convert", "json", "-o", "-", strings_path], capture_output=True, check=True, text=True, ) return json.loads(result.stdout) def objc_string_literal(value): escaped = value.replace("\\", "\\\\").replace('"', '\\"') escaped = escaped.replace("\n", "\\n").replace("\t", "\\t") return '@"' + escaped + '"' def collect_localizations(): localizations = {} lproj_dirs = sorted(glob.glob(os.path.join(SPARKLE_DIR, "*.lproj"))) for lproj_dir in lproj_dirs: lproj_name = os.path.basename(lproj_dir)[: -len(".lproj")] locale = LPROJ_LOCALE_OVERRIDES.get(lproj_name, lproj_name) strings_path = os.path.join(lproj_dir, "Sparkle.strings") if not os.path.isfile(strings_path): continue table = load_strings(strings_path) localized_values = {} for key in REQUIRED_KEYS: value = table.get(key) if value is None: print("warning: %s.lproj/Sparkle.strings is missing key %r" % (lproj_name, key), file=sys.stderr) continue localized_values[key] = value if localized_values: localizations[locale] = localized_values return localizations def generate_header(localizations): lines = [] lines.append("//") lines.append("// SPUInstallerProgressLocalizedStrings.h") lines.append("// Sparkle") lines.append("//") lines.append("// This file is auto-generated by Configurations/generate_progress_tool_localizations.py") lines.append("// Do not edit by hand. Re-run the script after changing the strings below in") lines.append("// Sparkle/*.lproj/Sparkle.strings") lines.append("//") lines.append("") lines.append("#import ") lines.append("") lines.append("static NSDictionary *> *const SPUInstallerProgressLocalizedStringsTable = @{") for locale in sorted(localizations.keys()): lines.append(' @"%s": @{' % locale) for key in REQUIRED_KEYS: value = localizations[locale].get(key) if value is None: continue lines.append(" %s: %s," % (objc_string_literal(key), objc_string_literal(value))) lines.append(" },") lines.append("};") lines.append("") return "\n".join(lines) def update_info_plist_localizations(locales): with open(INFO_PLIST, "rb") as f: plist = plistlib.load(f) sorted_locales = sorted(locales) if plist.get("CFBundleLocalizations") == sorted_locales: print("%s is already up to date (%d locales)" % (INFO_PLIST, len(sorted_locales))) return plist["CFBundleLocalizations"] = sorted_locales # sort_keys=False preserves the existing top-level key order, so only # CFBundleLocalizations's contents change in the diff with open(INFO_PLIST, "wb") as f: plistlib.dump(plist, f, fmt=plistlib.FMT_XML, sort_keys=False) print("Wrote %s (%d locales)" % (INFO_PLIST, len(sorted_locales))) def update_header(localizations): header_contents = generate_header(localizations) if os.path.isfile(OUTPUT_HEADER): with open(OUTPUT_HEADER, "r", encoding="utf-8") as f: if f.read() == header_contents: print("%s is already up to date (%d locales)" % (OUTPUT_HEADER, len(localizations))) return with open(OUTPUT_HEADER, "w", encoding="utf-8") as f: f.write(header_contents) print("Wrote %s (%d locales)" % (OUTPUT_HEADER, len(localizations))) def main(): localizations = collect_localizations() update_header(localizations) update_info_plist_localizations(localizations.keys()) if __name__ == "__main__": main()