forked from HamletDuFromage/aio-switch-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localizer.py
executable file
·78 lines (65 loc) · 2.73 KB
/
localizer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import json
import argparse
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def color_string(self, message):
return f"{self.OKCYAN}{message}{self.ENDC}"
class Localizer:
def __init__(self, reference_path, working_path):
self.reference_dict = dict()
self.working_dict = dict()
self.out_dict = dict()
self.reference_path = reference_path
self.working_path = working_path
self.init_dicts()
def init_dicts(self):
with open(self.reference_path, encoding="utf8") as reference_file:
self.reference_dict = json.load(reference_file)
with open(self.working_path, encoding="utf8") as working_file:
self.working_dict = json.load(working_file)
def get_new_dict(self):
self.out_dict = self.update_values(self.reference_dict, self.working_dict)[0]
def update_values(self, reference_dict, working_dict):
colors = Colors()
done = False
for key, value in reference_dict.items():
if done:
return (working_dict, True)
if isinstance(value, dict):
if key not in working_dict:
working_dict[key] = dict()
working_dict[key], done = self.update_values(
value, working_dict[key])
elif key not in working_dict:
working_val = input(
f"Translation for key: [{key}] was not found.\nType in the translation in the field below.\nType \"_exit\" to save & quit and \"_skip\" to skip to the next key.\n<<< {colors.color_string(repr(value)[1:-1])}\n>>> ")
if working_val == "_exit":
return (working_dict, True)
elif working_val == "_skip":
continue
working_dict[key] = working_val
return (working_dict, False)
def update_file(self):
self.get_new_dict()
with open(self.working_path, 'w') as working_file:
json.dump(self.out_dict, working_file, indent=2, ensure_ascii=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Compare and update JSON translation files")
required = parser.add_argument_group('Required arguments')
required.add_argument('-r', '--reference',
help='reference file', required=True)
required.add_argument(
'-w', '--working', help='working file, will be edited', required=True)
args = parser.parse_args()
loc = Localizer(args.reference, args.working)
loc.update_file()