Ticket #5751: trans_to_json.py

File trans_to_json.py, 846 bytes (added by tlucas, on 09/25/2017 at 11:19:18 AM)
Line 
1import os
2import io
3import sys
4
5from buildtools.localeTools import toJSON
6
7EXTENSIONS = {'dtd', 'properties'}
8
9
10def convert_all_translations(path):
11    trans_files = []
12    for root, folder, files in os.walk(path):
13        t_files = [f for f in files
14                   if any(f.endswith(ext) for ext in EXTENSIONS)]
15        for f in t_files:
16            trans_files.append(os.path.join(root, f))
17
18    for file_path in trans_files:
19        new_name = '.'.join(file_path.split('.')[:-1] + ['json'])
20        print 'converting {} -> {}'.format(
21            file_path, new_name
22        )
23        with io.open(new_name, 'w', encoding='utf-8') as fp:
24            fp.write(toJSON(file_path))
25
26if __name__ == '__main__':
27    if len(sys.argv) > 1:
28        convert_all_translations(sys.argv[1])
29    else:
30        print 'Please provide a translation path.'