comparison htdocs/svg-edit/extras/update-langs.py @ 480:1ef31d76eb88

updated to version of svg-edit Revision r1310
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 31 Jan 2010 18:35:04 +0100
parents
children 55ffbd41c42d
comparison
equal deleted inserted replaced
479:167888214d95 480:1ef31d76eb88
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # This is a helper script for the svg-edit project, useful for managing
5 # all the language files
6 #
7 # Licensed under the Apache 2 License as is the rest of the project
8 # Requires Python 2.6
9 #
10 # Copyright (c) 2010 Jeff Schiller
11
12 import os
13 import sys
14 import re
15 import json
16 from types import *
17
18 def updateMainMenu(j):
19 """
20 Converts title into textContent for items in the main menu
21 """
22 tools = ['tool_clear', 'tool_open', 'tool_save', 'tool_docprops']
23 for row in j:
24 try:
25 id = row['id']
26 if id in tools:
27 row[u'textContent'] = row.pop('title')
28 except KeyError:
29 pass
30
31 def ourPrettyPrint(j):
32 """
33 Outputs a string representation of the JSON object passed in
34 formatted properly for our lang.XX.js files.
35 """
36 s = '[' + os.linesep
37 js_strings = None
38 j.sort()
39 for row in j:
40 try:
41 id = row['id']
42 row_string = json.dumps(row, sort_keys=True, ensure_ascii=False)
43 s += row_string + ',' + os.linesep
44 except KeyError:
45 if type(row) is DictType:
46 js_strings = row
47
48 s += json.dumps(js_strings, sort_keys=True, ensure_ascii=False, indent=1) + os.linesep
49 s += ']'
50 return s
51
52 def processFile(filename):
53 """
54 Loads the given lang.XX.js file, processes it and saves it
55 back to the file system
56 """
57 in_file = open('./editor/locale/'+filename, 'r')
58 in_string = in_file.read()
59 in_file.close()
60
61 try:
62 j = json.loads(in_string)
63
64 # process the JSON object here
65 # updateMainMenu(j)
66
67 # now write it out back to the file
68 s = ourPrettyPrint(j).encode("UTF-8");
69 out_file = open('./editor/locale/'+filename, 'w')
70 out_file.write( s )
71 out_file.close()
72
73 print "Updated " + filename
74 except ValueError:
75 print "ERROR! " + filename + " was not valid JSON, please fix it!"
76
77 if __name__ == '__main__':
78 # get list of all lang files and process them
79 for file in os.listdir('./editor/locale/'):
80 if file[:4] == "lang":
81 processFile(file)