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