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
36
38 contextancestors = dict.fromkeys(["TS"])
39 messageancestors = dict.fromkeys(["TS", "context"])
40
42 """make a new QtTsParser, reading from the given inputfile if required"""
43 self.filename = getattr(inputfile, "filename", None)
44 self.knowncontextnodes = {}
45 self.indexcontextnodes = {}
46 if inputfile is None:
47 self.document = ourdom.parseString("<!DOCTYPE TS><TS></TS>")
48 else:
49 self.document = ourdom.parse(inputfile)
50 assert self.document.documentElement.tagName == "TS"
51
52 - def addtranslation(self, contextname, source, translation, comment=None, transtype=None, createifmissing=False):
53 """adds the given translation (will create the nodes required if asked). Returns success"""
54 contextnode = self.getcontextnode(contextname)
55 if contextnode is None:
56 if not createifmissing:
57 return False
58
59 contextnode = self.document.createElement("context")
60 namenode = self.document.createElement("name")
61 nametext = self.document.createTextNode(contextname)
62 namenode.appendChild(nametext)
63 contextnode.appendChild(namenode)
64 self.document.documentElement.appendChild(contextnode)
65 if not createifmissing:
66 return False
67 messagenode = self.document.createElement("message")
68 sourcenode = self.document.createElement("source")
69 sourcetext = self.document.createTextNode(source)
70 sourcenode.appendChild(sourcetext)
71 messagenode.appendChild(sourcenode)
72 if comment:
73 commentnode = self.document.createElement("comment")
74 commenttext = self.document.createTextNode(comment)
75 commentnode.appendChild(commenttext)
76 messagenode.appendChild(commentnode)
77 translationnode = self.document.createElement("translation")
78 translationtext = self.document.createTextNode(translation)
79 translationnode.appendChild(translationtext)
80 if transtype:
81 translationnode.setAttribute("type", transtype)
82 messagenode.appendChild(translationnode)
83 contextnode.appendChild(messagenode)
84 return True
85
87 """return the ts file as xml"""
88 xml = self.document.toprettyxml(indent=" ", encoding="utf-8")
89
90 xml = "\n".join([line for line in xml.split("\n") if line.strip()])
91 return xml
92
93 - def getcontextname(self, contextnode):
94 """returns the name of the given context"""
95 namenode = ourdom.getFirstElementByTagName(contextnode, "name")
96 return ourdom.getnodetext(namenode)
97
98 - def getcontextnode(self, contextname):
99 """finds the contextnode with the given name"""
100 contextnode = self.knowncontextnodes.get(contextname, None)
101 if contextnode is not None:
102 return contextnode
103 contextnodes = self.document.searchElementsByTagName("context", self.contextancestors)
104 for contextnode in contextnodes:
105 if self.getcontextname(contextnode) == contextname:
106 self.knowncontextnodes[contextname] = contextnode
107 return contextnode
108 return None
109
121
126
131
133 """returns the message translation attributes for a given node"""
134 translationnode = ourdom.getFirstElementByTagName(message, "translation")
135 return translationnode.getAttribute("type")
136
143
148
150 """clean up the document if required"""
151 if hasattr(self, "document"):
152 self.document.unlink()
153