3196465934e385bd03d3db9185a3c7761d934cc6
[public/netxms.git] / tools / sync_message_files.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import collections
6
7 locales = ['cs', 'es', 'ru', 'zh_CN']
8
9 def readPropertyFile(name):
10 data = {}
11 try:
12 f = open(name)
13 for line in f.readlines():
14 line = line.rstrip("\r\n")
15 s = line.split("=")
16 if len(s) == 2:
17 data[s[0].strip()] = s[1]
18 except IOError:
19 data = {}
20 return data
21
22 def processDirectory(directory, prefix):
23 source = readPropertyFile(os.path.join(directory, "%s.properties" % (prefix, )))
24 for locale in locales:
25 translationFileName = os.path.join(directory, "%s_%s.properties" % (prefix, locale, ))
26 translation = readPropertyFile(translationFileName)
27 # hack
28 if prefix == 'messages':
29 filteredTranslation = {}
30 for e in translation.iteritems():
31 filteredTranslation[e[0].replace('.', '_')] = e[1]
32 translation = filteredTranslation
33 for k in set(translation.keys()) - set(source.keys()):
34 del translation[k]
35 translation = dict(source.items() + translation.items())
36 translation = collections.OrderedDict(sorted(translation.items()))
37 f = open(translationFileName, 'w')
38 for key in translation:
39 f.write("%s=%s\n" % (key, translation[key]))
40 f.close()
41
42
43 def main():
44 for (root, dirs, files) in os.walk(sys.argv[1]):
45 if '.svn' in dirs:
46 dirs.remove('.svn')
47 if "messages.properties" in files:
48 processDirectory(root, "messages")
49 if "bundle.properties" in files:
50 processDirectory(root, "bundle")
51
52 if len(sys.argv) == 2:
53 main()
54 else:
55 print "Usage: ./sync_message_files.py <source root>"