devutils/i18n: add clean command (#1869)

This commit is contained in:
jj authored and GitHub committed 2026-06-05 19:23:15 +02:00
1 parent 7a32267ee4
commit 5b7dd06c6f
2 files changed
+59

No files matched your search

+6
View File
@@ -48,6 +48,8 @@ def parse_args():
help='Import translations from a JSON file '
'instead of calling the LLM.')
subparsers.add_parser('clean', help='Clean up stale translation strings')
return parser.parse_args()
@@ -63,6 +65,10 @@ def main():
import i18n_translate # pylint: disable=import-outside-toplevel
return i18n_translate.run(args)
if args.command == 'clean':
import i18n_clean # pylint: disable=import-outside-toplevel
return i18n_clean.run()
raise ValueError(f'unknown command: {args.command}')
+53
View File
@@ -0,0 +1,53 @@
# Copyright 2026 The Helium Authors
# You can use, redistribute, and/or modify this source code under
# the terms of the GPL-3.0 license that can be found in the LICENSE file.
"""Helper for cleaning up obsolete and outdated translation strings."""
import json
from pathlib import Path
I18N_DIR = Path(__file__).resolve().parent.parent / 'i18n'
SOURCE_PATH = I18N_DIR / 'source.gen.json'
TRANSLATIONS_DIR = I18N_DIR / 'translations'
def clean_translation_file(lang, source_keys):
"""Clean up outdated and duplicated translations in a language file."""
path = TRANSLATIONS_DIR / f'{lang}.json'
with open(path, encoding='utf-8') as file:
entries = json.load(file)
translation_keys = set()
result = []
n_filtered = 0
for entry in entries:
key = (entry['name'], entry['source'])
if key in source_keys and key not in translation_keys:
result.append(entry)
translation_keys.add(key)
else:
n_filtered += 1
if n_filtered > 0:
with open(path, 'w', encoding='utf-8') as file:
json.dump(result, file, indent=2, ensure_ascii=False)
file.write('\n')
return n_filtered
def run():
"""Clean up outdated translation strings."""
with open(SOURCE_PATH, encoding='utf-8') as file:
source = json.load(file)
with open(I18N_DIR / 'languages.json', encoding='utf-8') as file:
languages = json.load(file)
existing_keys = set()
for entry in source:
existing_keys.add((entry['name'], entry['message']))
for lang in languages:
n_filtered = clean_translation_file(lang, existing_keys)
if n_filtered:
print(f'{lang}: filtered {n_filtered} string(s)')