-
Notifications
You must be signed in to change notification settings - Fork 2
/
json-to-strings.py
42 lines (35 loc) · 1.18 KB
/
json-to-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
import argparse
import json
import csv
import sys
import re
assert sys.version_info >= (3, 6), "Python >= 3.6 is required"
def dict_to_strings(d, out_path):
with open(out_path, 'w') as out_file:
for key, text in d.items():
text = re.sub(
r'\{(\w+)\}',
r'%@',
text,
)
# Newlines should be escaped
text = text.replace("\n", "\\n")
# Single quotes should be escaped
text = text.replace("\"", "\\\"")
out_file.write('"{}" = "{}";'.format(key, text))
out_file.write('\n')
def load_json(in_path):
with open(in_path) as in_file:
return json.load(in_file)
def parse_args():
p = argparse.ArgumentParser(description='translations: CSV to KEYVALUEJSON')
p.add_argument('--json', metavar='PATH', help='json input path', required=True)
p.add_argument('--strings', metavar='PATH', help='strings output path', required=True)
opt = p.parse_args()
return opt
def main():
opt = parse_args()
dict_to_strings(load_json(opt.json), opt.strings)
print("{} written".format(opt.strings))
if __name__ == "__main__":
main()