mirror of
https://github.com/YunoHost/yunohost-portal.git
synced 2024-09-03 20:06:23 +02:00
maintenance: add clean_locale python script
This commit is contained in:
parent
266a1a8866
commit
dbe447fcd0
1 changed files with 79 additions and 0 deletions
79
maintenance/clean_locales.py
Normal file
79
maintenance/clean_locales.py
Normal 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()
|
Loading…
Reference in a new issue