Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import json
- import os
- # All scripts must be in the same directory as the ui directory, as they need to search through it.
- #
- # First script:
- #
- # This script takes each key from the English json and searches for that snippet in all the JS, VUE and HTML files in
- # the UI folder, saving in another file all the keys that are used.
- walk_dir = 'ui'
- keyArray = []
- with open('./keylist.txt', 'w') as keylist:
- for root, subdirs, files in os.walk(walk_dir):
- for filename in files:
- if filename.endswith('.js') or filename.endswith('.vue') or filename.endswith('html'):
- file_path = os.path.join(root, filename)
- print(file_path)
- with open(file_path, 'r') as f:
- with open("ui/public/locales/en.json", 'r') as j:
- en = json.loads(j.read())
- for key, value in en.items():
- f.seek(0)
- for line in f:
- if key.lstrip('label.') in line:
- if key not in keyArray:
- keyArray.append(key)
- keylist.write(key)
- keylist.write(' ')
- # Second script:
- #
- # This script takes all the keys in en.json that aren't in the first script's output and saves them
- # to another file. Which therefore contains all the keys that are not being used according to the first script.
- with open('./keylist.txt', 'r') as keylist:
- with open('./unused.txt', 'w') as unused:
- with open("ui/public/locales/en.json", 'r') as j:
- en = json.loads(j.read())
- for key in en.keys():
- keylist.seek(0)
- if key not in keylist.readline().split(' '):
- unused.write(key)
- unused.write(' ')
- # Third script:
- #
- # This script was made to ensure that all the keys discovered in the second script were not really being used,
- # for that purpose it takes each of the keys discovered in the second script and calls grep -r -l -i '<key>',
- # saving in another file all the results.
- with open('./unused.txt', 'r') as un:
- with open('./verifyManually.txt', 'w') as ver:
- for key in un.readline().split(' '):
- result = subprocess.getoutput("grep -r -l -i {keyvalue}".format(keyvalue=key))
- print('\n', file=ver)
- print(key, file=ver)
- for line in result.split('\n'):
- if (not line.endswith('.json')) and (not line.endswith('.txt')):
- print(line, file=ver)
- if 'label.' in key:
- result = subprocess.getoutput("grep -r -l -i {keyvalue}".format(keyvalue=key.lstrip('label.')))
- for line in result.split('\n'):
- if (not line.endswith('.json')) and (not line.endswith('.txt')):
- print(line, file=ver)
- # Fourth script:
- #
- # In order for this one to work, there needs to be a manual step between this and the third script.
- # One must search through the verifyManually.txt file and delete all the labels that have any result from the "grep
- # -r -l -i" alongside it's results, and then use a tool to remove all linebreaks and paragraphs from the file so
- # that all the keys are in one line, and separated by one whitespace. This is the site I used:
- # https://www.textfixer.com/tools/remove-line-breaks.php
- with open('./verifyManually.txt', 'r') as f:
- with open('ui/public/locales/en.json', encoding='utf8') as j:
- data = json.load(j)
- for label in f.readline().split(' '):
- for element in list(data):
- if label.__eq__(element):
- data.pop(element, None)
- with open('./updated-en.json', 'w', encoding='utf8') as nj:
- json.dump(data, nj, indent=0)
- # Fifth script:
- #
- # This script simply removes any keys from the other languages' json that weren't in the updated English file.
- #
- # Ps: in order for this to work properly, one must replace the ui/public/locales/en.json with the updated-en.json
- # generated by the last script.
- walk_dir = 'ui/public/locales'
- keyArray = []
- for root, subdirs, files in os.walk(walk_dir):
- for filename in files:
- if filename == 'en.json':
- continue
- file_path = os.path.join(root, filename)
- print(file_path)
- with open("ui/public/locales/en.json", 'r') as j:
- en = json.loads(j.read())
- enKeys = en.keys()
- with open(file_path, 'r+') as f:
- lang = json.loads(f.read())
- for key, value in list(lang.items()):
- if key not in enKeys:
- lang.pop(key, None)
- f.seek(0)
- json.dump(lang, f, indent=0)
- f.truncate()
- # Final script: Since not all the jsons are encoded in utf8, in order to maintain their original encoding,
- # this script was used manually in each of the jsons that had their encodings changed.
- with open('ui/public/locales/hi.json', 'r+') as f:
- lang = json.loads(f.read())
- f.seek(0)
- json.dump(lang, f, indent=0, ensure_ascii=False)
Advertisement
Add Comment
Please, Sign In to add comment