Message migration tool fixed
[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 = ['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]] = 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 filteredTranslation = {}
28 for e in translation.iteritems():
29 filteredTranslation[e[0].replace('.', '_')] = e[1]
30 translation = filteredTranslation
31 for k in set(translation.keys()) - set(source.keys()):
32 del translation[k]
33 translation = dict(source.items() + translation.items())
34 translation = collections.OrderedDict(sorted(translation.items()))
35 f = open(translationFileName, 'w')
36 for key in translation:
37 f.write("%s=%s\n" % (key, translation[key]))
38 f.close()
39
40
41 def main():
42 for (root, dirs, files) in os.walk(sys.argv[1]):
43 if '.svn' in dirs:
44 dirs.remove('.svn')
45 if "messages.properties" in files:
46 processDirectory(root, "messages")
47 if "bundle.properties" in files:
48 processDirectory(root, "bundle")
49
50 if len(sys.argv) == 2:
51 main()
52 else:
53 print "Usage: ./sync_message_files.py <source root>"