From b9f2c7d7e520c461c7440bdcede6fabc0eebd26c Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 20 Aug 2020 14:54:05 +1000 Subject: [PATCH] update locale tool to find and clean unused key in .json from english --- tools/compareLocalizedStrings.py | 46 +++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/tools/compareLocalizedStrings.py b/tools/compareLocalizedStrings.py index 3b72b6711..71e5b06e2 100755 --- a/tools/compareLocalizedStrings.py +++ b/tools/compareLocalizedStrings.py @@ -4,27 +4,43 @@ # usage : ./tools/compareLocalizedStrings.py en de import re -import os +from os import path, listdir from glob import glob import json import sys -if len(sys.argv) != 3: - print(f'usage: {sys.argv[0]} ') +LOCALES_FOLDER = './_locales' + + +if len(sys.argv) != 1 and len(sys.argv) != 2: + print(f'usage: {sys.argv[0]} . If compared locale is not set, all found locales will be cleaned') sys.exit(1) -src = sys.argv[1] -dest = sys.argv[2] -print(f'src {src}, dest {dest}') -jsonInEn = json.loads(open(f'_locales/{src}/messages.json', 'r').read()) -jsonInDe = json.loads(open(f'_locales/{dest}/messages.json', 'r').read()) -keysInEn = jsonInEn.keys() -keysInDe = jsonInDe.keys() +if (len(sys.argv) > 1): + localesToCheck = [f'{LOCALES_FOLDER}{sys.argv[1]}'] +else: + localesToCheck = [path.join(LOCALES_FOLDER, o) for o in listdir(LOCALES_FOLDER) if path.isdir(path.join(LOCALES_FOLDER,o))] + +for dest in localesToCheck: + if dest == f'{LOCALES_FOLDER}/en': + print('skipping "en" locale...') + continue + + destFilePath = f'{dest}/messages.json' + # print(f'dest {dest}, destFilePath {destFilePath}') + + jsonInSrc = json.loads(open(f'{LOCALES_FOLDER}/en/messages.json', 'r').read()) + jsonInDest = json.loads(open(destFilePath, 'r').read()) + keysInSrc = jsonInSrc.keys() + keysInDest = jsonInDest.keys() -print(keysInEn) + destMinusSrc = list(set(keysInDest) - set(keysInSrc)) + for key in destMinusSrc: + # print(f'Present in "{dest}" but not found in "{src}": {key}') + del jsonInDest[key] -for key in keysInEn: - if key not in keysInDe: - print(f'not found in de:{key}') + print(f'total keys in "{dest}" to remove: {len(destMinusSrc)}') -print(f'total keys in en {len(keysInEn)}') \ No newline at end of file + # write the updated json dict to the file + with open(destFilePath, 'w') as outfile: + json.dump(jsonInDest, outfile, indent=4, ensure_ascii=False)