annotate htdocs/svg-edit/extras/update-langs.py @ 515:55ffbd41c42d

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