maintenance: add clean_locale python script

This commit is contained in:
axolotle 2023-11-14 16:48:40 +01:00
parent 266a1a8866
commit dbe447fcd0

View file

@ -0,0 +1,79 @@
import json
from collections import OrderedDict
from pathlib import Path
from typing import Any
# FIXME use Path
ROOT_FOLDER = Path(__file__).parent.parent
LOCALES_FOLDER = ROOT_FOLDER / "locales"
LOCALES_LIST_FILE = ROOT_FOLDER / "maintenance" / "locales.json"
LOCALE_FILES = {path.stem: path for path in LOCALES_FOLDER.glob("*.json")}
ALL_LOCALES = set(LOCALE_FILES.keys())
def get_json(path: Path) -> Any:
return json.loads(path.read_text(), object_pairs_hook=OrderedDict)
def save_json(path: Path, data: Any, sort: bool = False) -> None:
with path.open("w", encoding="utf-8") as fp:
json.dump(data, fp, ensure_ascii=False, sort_keys=sort, indent=4)
fp.write("\n")
def get_flatten_keys(d, parent_key=""):
items = set()
for k, v in d.items():
key = f"{parent_key}.{k}" if parent_key else k
items.add(key)
if isinstance(v, dict):
items |= get_flatten_keys(v, key)
return items
def filter_data(data, stale_keys):
filtered = OrderedDict()
for k, v in data.items():
if k not in stale_keys:
value = v
if isinstance(v, OrderedDict):
nested_stale_keys = [
key.replace(f"{k}.", "")
for key in stale_keys
if key.startswith(f"{k}.")
]
if nested_stale_keys:
value = filter_data(v, nested_stale_keys)
filtered[k] = value
return filtered
def remove_stale_translations(ref="en", locales=None):
locales = set(locales) if locales else set(ALL_LOCALES)
locales.discard(ref)
ref_path = LOCALE_FILES[ref]
ref_data = get_json(ref_path)
ref_keys = get_flatten_keys(ref_data)
for locale in locales:
path = LOCALE_FILES[locale]
data = get_json(path)
keys = get_flatten_keys(data)
stale_keys = sorted(keys - ref_keys)
filtered_data = filter_data(data, stale_keys)
save_json(path, filtered_data)
# Resave ref file to sort and indent
save_json(ref_path, ref_data, sort=True)
if __name__ == "__main__":
remove_stale_translations()