forked from Yellow-Dog-Man/Locale
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CleanJSON.py
75 lines (63 loc) · 2.56 KB
/
CleanJSON.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
#!/usr/bin/env python3
import json
class LocaleCleaner:
def __init__(self, en, lang, out):
en_file = open(en, 'r', encoding="utf8")
lang_file = open(lang, 'r', encoding="utf8")
self.out = out
self.en = en_file.readlines()
self.lang = json.load(lang_file)
self.output = []
def run(self):
self.make_header()
self.parse()
self.make_footer()
self.save()
def make_header(self):
self.output.append('{')
self.output.append(' "localeCode": "{}",'.format(self.lang["localeCode"]))
self.output.append(' "authors": ["{}"],'.format('", "'.join(self.lang["authors"])))
self.output.append(' "messages": {')
def make_footer(self):
self.output.append(' "Dummy": "Dummy"')
self.output.append(' }')
self.output.append('}')
self.output.append('')
def find_start(self):
counter = 0
for line in self.en:
counter += 1
if "message" in line:
break
return counter
def parse(self):
start_pos = self.find_start()
blank = False
for line in self.en[start_pos:]:
if '"Dummy": "Dummy"' in line:
break
key = line.strip().split(':')[0].strip().replace('"','')
if key in self.lang["messages"]:
blank = False
translation = self.lang["messages"][key].replace('\n','\\n').replace('"','\\"')
self.output.append(' "{}": "{}",'.format(key, translation))
elif blank == False:
self.output.append('')
blank = True
def save(self):
out = open(self.out, 'w', encoding="utf8")
out.write('\n'.join(self.output))
out.close()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='This script will reformat a Babel style JSON for locales to match the en.json baseline formatting for git changes purposes.')
parser.add_argument('--en', metavar='en_path', type=str,
help='The path to the en.json locale.')
parser.add_argument('--lang', metavar='lang_path', type=str,
help='The path to the LANG.json locale to clean.')
parser.add_argument('--out', metavar='out_path', type=str,
help='The path to save the formatted file.')
args = parser.parse_args()
N = LocaleCleaner(args.en, args.lang, args.out)
N.run()
print("Cleaned!")