update hu,nl language
[enigma2.git] / po / xml2po.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import string
5 from xml.sax import make_parser
6 from xml.sax.handler import ContentHandler, property_lexical_handler
7 try:
8         from _xmlplus.sax.saxlib import LexicalHandler
9         no_comments = False
10 except ImportError:
11         class LexicalHandler:
12                 pass
13         no_comments = True
14
15 class parseXML(ContentHandler, LexicalHandler):
16         def __init__(self, attrlist):
17                 self.isPointsElement, self.isReboundsElement = 0, 0
18                 self.attrlist = attrlist
19                 self.last_comment = None
20
21         def comment(self, comment):
22                 if comment.find("TRANSLATORS:") != -1:
23                         self.last_comment = comment
24
25         def startElement(self, name, attrs):
26                 for x in ["text", "title", "value", "caption"]:
27                         try:
28                                 attrlist.add((attrs[x], self.last_comment))
29                                 self.last_comment = None
30                         except KeyError:
31                                 pass
32
33 parser = make_parser()
34
35 attrlist = set()
36
37 contentHandler = parseXML(attrlist)
38 parser.setContentHandler(contentHandler)
39 if not no_comments:
40         parser.setProperty(property_lexical_handler, contentHandler)
41 dir = os.listdir(sys.argv[1])
42 for x in dir:
43         if (str(x[-4:]) == ".xml"):
44                 parser.parse(sys.argv[1] + str(x))
45
46 #parser.parse(sys.argv[1])
47
48 attrlist = list(attrlist)
49 attrlist.sort(key=lambda a: a[0])
50
51 for (k,c) in attrlist:
52         print
53         print '#: ' + sys.argv[1]
54         string.replace(k, "\\n", "\"\n\"")
55         if c:
56                 for l in c.split('\n'):
57                         print "#. ", l
58         if str(k) != "":
59                 print 'msgid "' + str(k) + '"'
60                 print 'msgstr ""'
61