-
Notifications
You must be signed in to change notification settings - Fork 52
/
find_missing_i18n_strings.py
46 lines (40 loc) · 1.64 KB
/
find_missing_i18n_strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import re
import os
import json
# Open the json file and load its contents into a dictionary
with open('i18n/en.json', 'r', encoding='utf-8') as f:
en = json.load(f)
# Create a copy of the en dictionary's keys and convert it to a set for better performance
unused_keys = set(en.keys())
error = False
missing_keys = {}
rootdir = '.'
# TODO: Crawl only content/*
for folder, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith('.html'):
fullpath = os.path.join(folder, file)
with open(fullpath, 'r', encoding='utf-8') as f:
for line in f:
m = re.findall('{{\s+?i18n\s+?(?:"|`)(.*?)(?:"|`)\s+?}}', line, re.DOTALL)
if m:
for string in m:
if string not in en:
error = True
print(f'TRANSLATION ERROR: {string}')
missing_keys[string] = ''
print(f"Adding '{string}'")
en[string] = string # Add the missing key to the dictionary
elif string in unused_keys:
unused_keys.remove(string)
# If there are missing keys, dump the updated dictionary back into the json file
if error or unused_keys:
# Remove unused keys
if unused_keys:
for key in unused_keys:
print(f"Removing '{key}'")
del en[key]
else:
print("No unused keys found.")
with open('i18n/en.json', 'w', encoding='utf-8') as f:
json.dump(en, f, indent=3, ensure_ascii=False)