From f07633630f184b85bb6ca4a1e56f983df76ed6ef Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Thu, 30 Nov 2023 15:15:59 +0100 Subject: [PATCH] Re-format arb files on commit. --- .pre-commit-config.yaml | 10 ++++++++-- arb_reformatter.py | 37 +++++++++++++++++++++++-------------- check_strings.py | 4 ++-- lib/l10n/app_de.arb | 1 + lib/l10n/app_fr.arb | 1 + lib/l10n/app_ja.arb | 1 + 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2350d840..78526768 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,6 +8,14 @@ repos: require_serial: true - id: flutter-analyze require_serial: true +- repo: local + hooks: + - id: arb-reformatter + name: reformat-strings + files: \.arb$ + language: script + entry: arb_reformatter.py + require_serial: true # Python - repo: local @@ -22,12 +30,10 @@ repos: rev: 6.1.0 hooks: - id: flake8 - files: helper/ - repo: https://github.com/psf/black rev: 23.11.0 hooks: - id: black - files: helper/ - repo: https://github.com/PyCQA/bandit rev: 1.7.5 hooks: diff --git a/arb_reformatter.py b/arb_reformatter.py index 0680edab..d3cf4ca0 100755 --- a/arb_reformatter.py +++ b/arb_reformatter.py @@ -5,24 +5,33 @@ import os def read_file_lines(file_path): - with open(file_path, 'r', encoding='utf-8') as file: + with open(file_path, "r", encoding="utf-8") as file: return file.readlines() def read_file_json(file_path): - with open(file_path, 'r', encoding='utf-8') as file: + with open(file_path, "r", encoding="utf-8") as file: return json.load(file) def write_to_file(file_path, text): - with open(file_path, 'w', encoding='utf-8') as file: + with open(file_path, "r", encoding="utf-8") as file: + if file.read() == text: + return False + + with open(file_path, "w", encoding="utf-8") as file: file.write(text) + return True + # Translation table for unicode characters we want to keep in escaped form. -trans = str.maketrans({ - '\u00a0': r"\u00a0", # No-Break Space (NBSP) - '\u2026': r"\u2026" # Horizontal Ellipsis -}) +trans = str.maketrans( + { + "\u00a0": r"\u00a0", # No-Break Space (NBSP) + "\u2026": r"\u2026", # Horizontal Ellipsis + } +) + # Move keys in target into same order as in source. # Keys not present in source are removed from target. @@ -68,16 +77,16 @@ def update_arb_file(source_path, target_path, language_code): if line.strip() == "": target_lines.insert(i, "") - write_to_file(target_path, "\n".join(target_lines).strip() + "\n") + return write_to_file(target_path, "\n".join(target_lines).strip() + "\n") if __name__ == "__main__": - source_file_path = 'lib/l10n/app_en.arb' - target_directory = 'lib/l10n' + source_file_path = "lib/l10n/app_en.arb" + target_directory = "lib/l10n" for file_name in os.listdir(target_directory): - if file_name.startswith('app_') and file_name.endswith('.arb'): + if file_name.startswith("app_") and file_name.endswith(".arb"): target_file_path = os.path.join(target_directory, file_name) - language_code = file_name.split('_')[1].split('.')[0] - update_arb_file(source_file_path, target_file_path, language_code) - print(f'File updated: {file_name}') + language_code = file_name.split("_")[1].split(".")[0] + if update_arb_file(source_file_path, target_file_path, language_code): + print(f"File updated: {file_name}") diff --git a/check_strings.py b/check_strings.py index 535f1837..cd7fc4f1 100755 --- a/check_strings.py +++ b/check_strings.py @@ -106,7 +106,7 @@ if len(sys.argv) != 2: target = sys.argv[1] -with open(target, encoding='utf-8') as f: +with open(target, encoding="utf-8") as f: values = json.load(f, object_pairs_hook=check_duplicate_keys) strings = {k: v for k, v in values.items() if not k.startswith("@")} @@ -115,7 +115,7 @@ print(target, f"- checking {len(strings)} strings") lint_strings(strings, values.get("@_lint_rules", {})) check_duplicate_values(strings) -with open(os.path.join(os.path.dirname(target), 'app_en.arb'), encoding='utf-8') as f: +with open(os.path.join(os.path.dirname(target), "app_en.arb"), encoding="utf-8") as f: reference_values = json.load(f) errors.extend(check_keys_exist_in_reference(reference_values, values)) diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index 5d1dac5b..486d1ed0 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -136,6 +136,7 @@ "@_app_configuration": {}, "s_toggle_applications": "Anwendungen umschalten", + "s_toggle_interfaces": null, "l_min_one_interface": "Mindestens ein Interface muss aktiviert sein", "s_reconfiguring_yk": "YubiKey wird neu konfiguriert\u2026", "s_config_updated": "Konfiguration aktualisiert", diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index bba609c0..fbe734e3 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -136,6 +136,7 @@ "@_app_configuration": {}, "s_toggle_applications": "Changer les applications", + "s_toggle_interfaces": null, "l_min_one_interface": "Au moins une interface doit être activée", "s_reconfiguring_yk": "Reconfiguration de la YubiKey\u2026", "s_config_updated": "Configuration mise à jour", diff --git a/lib/l10n/app_ja.arb b/lib/l10n/app_ja.arb index 03079ff3..9feef971 100644 --- a/lib/l10n/app_ja.arb +++ b/lib/l10n/app_ja.arb @@ -136,6 +136,7 @@ "@_app_configuration": {}, "s_toggle_applications": "アプリケーションの切替え", + "s_toggle_interfaces": null, "l_min_one_interface": "少なくとも 1 つのインターフェイスを有効にする必要があります", "s_reconfiguring_yk": "YubiKeyを再構成しています\u2026", "s_config_updated": "構成が更新されました",