1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for parsing Qt .ts files for translation.
24
25 Currently this module supports the old format of .ts files. Some applictaions
26 use the newer .ts format which are documented here:
27 U{TS file format 4.3<http://doc.trolltech.com/4.3/linguist-ts-file-format.html>},
28 U{Example<http://svn.ez.no/svn/ezcomponents/trunk/Translation/docs/linguist-format.txt>}
29
30 U{Specification of the valid variable entries <http://doc.trolltech.com/4.3/qstring.html#arg>},
31 U{2 <http://doc.trolltech.com/4.3/qstring.html#arg-2>}
32 """
33
34 from translate.misc import ourdom
35
37 contextancestors = dict.fromkeys(["TS"])
38 messageancestors = dict.fromkeys(["TS", "context"])
40 """make a new QtTsParser, reading from the given inputfile if required"""
41 self.filename = getattr(inputfile, "filename", None)
42 self.knowncontextnodes = {}
43 self.indexcontextnodes = {}
44 if inputfile is None:
45 self.document = ourdom.parseString("<!DOCTYPE TS><TS></TS>")
46 else:
47 self.document = ourdom.parse(inputfile)
48 assert self.document.documentElement.tagName == "TS"
49
50 - def addtranslation(self, contextname, source, translation, comment=None, transtype=None, createifmissing=False):
51 """adds the given translation (will create the nodes required if asked). Returns success"""
52 contextnode = self.getcontextnode(contextname)
53 if contextnode is None:
54 if not createifmissing:
55 return False
56
57 contextnode = self.document.createElement("context")
58 namenode = self.document.createElement("name")
59 nametext = self.document.createTextNode(contextname)
60 namenode.appendChild(nametext)
61 contextnode.appendChild(namenode)
62 self.document.documentElement.appendChild(contextnode)
63 if not createifmissing:
64 return False
65 messagenode = self.document.createElement("message")
66 sourcenode = self.document.createElement("source")
67 sourcetext = self.document.createTextNode(source)
68 sourcenode.appendChild(sourcetext)
69 messagenode.appendChild(sourcenode)
70 if comment:
71 commentnode = self.document.createElement("comment")
72 commenttext = self.document.createTextNode(comment)
73 commentnode.appendChild(commenttext)
74 messagenode.appendChild(commentnode)
75 translationnode = self.document.createElement("translation")
76 translationtext = self.document.createTextNode(translation)
77 translationnode.appendChild(translationtext)
78 if transtype:
79 translationnode.setAttribute("type", transtype)
80 messagenode.appendChild(translationnode)
81 contextnode.appendChild(messagenode)
82 return True
83
85 """return the ts file as xml"""
86 xml = self.document.toprettyxml(indent=" ", encoding="utf-8")
87
88 xml = "\n".join([line for line in xml.split("\n") if line.strip()])
89 return xml
90
91 - def getcontextname(self, contextnode):
92 """returns the name of the given context"""
93 namenode = ourdom.getFirstElementByTagName(contextnode, "name")
94 return ourdom.getnodetext(namenode)
95
96 - def getcontextnode(self, contextname):
97 """finds the contextnode with the given name"""
98 contextnode = self.knowncontextnodes.get(contextname, None)
99 if contextnode is not None:
100 return contextnode
101 contextnodes = self.document.searchElementsByTagName("context", self.contextancestors)
102 for contextnode in contextnodes:
103 if self.getcontextname(contextnode) == contextname:
104 self.knowncontextnodes[contextname] = contextnode
105 return contextnode
106 return None
107
119
124
129
131 """returns the message translation attributes for a given node"""
132 translationnode = ourdom.getFirstElementByTagName(message, "translation")
133 return translationnode.getAttribute("type")
134
141
146
148 """clean up the document if required"""
149 if hasattr(self, "document"):
150 self.document.unlink()
151