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  U{TS file format 4.3<http://doc.trolltech.com/4.3/linguist-ts-file-format.html>},  
 26  U{Example<http://svn.ez.no/svn/ezcomponents/trunk/Translation/docs/linguist-format.txt>} 
 27   
 28  U{Specification of the valid variable entries <http://doc.trolltech.com/4.3/qstring.html#arg>},  
 29  U{2 <http://doc.trolltech.com/4.3/qstring.html#arg-2>} 
 30   
 31  """ 
 32   
 33  from translate.misc import ourdom 
 34   
 36      contextancestors = dict.fromkeys(["TS"]) 
 37      messageancestors = dict.fromkeys(["TS", "context"]) 
 39          """make a new QtTsParser, reading from the given inputfile if required""" 
 40          self.filename = getattr(inputfile, "filename", None) 
 41          self.knowncontextnodes = {} 
 42          self.indexcontextnodes = {} 
 43          if inputfile is None: 
 44              self.document = ourdom.parseString("<!DOCTYPE TS><TS></TS>") 
 45          else: 
 46              self.document = ourdom.parse(inputfile) 
 47              assert self.document.documentElement.tagName == "TS" 
  48   
 49 -    def addtranslation(self, contextname, source, translation, comment=None, transtype=None, createifmissing=False): 
  50          """adds the given translation (will create the nodes required if asked). Returns success""" 
 51          contextnode = self.getcontextnode(contextname) 
 52          if contextnode is None: 
 53              if not createifmissing: 
 54                  return False 
 55               
 56              contextnode = self.document.createElement("context") 
 57              namenode = self.document.createElement("name") 
 58              nametext = self.document.createTextNode(contextname) 
 59              namenode.appendChild(nametext) 
 60              contextnode.appendChild(namenode) 
 61              self.document.documentElement.appendChild(contextnode) 
 62          if not createifmissing: 
 63              return False 
 64          messagenode = self.document.createElement("message") 
 65          sourcenode = self.document.createElement("source") 
 66          sourcetext = self.document.createTextNode(source) 
 67          sourcenode.appendChild(sourcetext) 
 68          messagenode.appendChild(sourcenode) 
 69          if comment: 
 70              commentnode = self.document.createElement("comment") 
 71              commenttext = self.document.createTextNode(comment) 
 72              commentnode.appendChild(commenttext) 
 73              messagenode.appendChild(commentnode) 
 74          translationnode = self.document.createElement("translation") 
 75          translationtext = self.document.createTextNode(translation) 
 76          translationnode.appendChild(translationtext) 
 77          if transtype: 
 78              translationnode.setAttribute("type",transtype) 
 79          messagenode.appendChild(translationnode) 
 80          contextnode.appendChild(messagenode) 
 81          return True 
  82   
 84          """return the ts file as xml""" 
 85          xml = self.document.toprettyxml(indent="    ", encoding="utf-8") 
 86           
 87          xml = "\n".join([line for line in xml.split("\n") if line.strip()]) 
 88          return xml 
  89   
 90 -    def getcontextname(self, contextnode): 
  91          """returns the name of the given context""" 
 92          namenode = ourdom.getFirstElementByTagName(contextnode, "name") 
 93          return ourdom.getnodetext(namenode) 
  94   
 95 -    def getcontextnode(self, contextname): 
  96          """finds the contextnode with the given name""" 
 97          contextnode = self.knowncontextnodes.get(contextname, None) 
 98          if contextnode is not None: 
 99              return contextnode 
100          contextnodes = self.document.searchElementsByTagName("context", self.contextancestors) 
101          for contextnode in contextnodes: 
102              if self.getcontextname(contextnode) == contextname: 
103                  self.knowncontextnodes[contextname] = contextnode 
104                  return contextnode 
105          return None 
 106   
118   
123   
128   
130          """returns the message translation attributes for a given node""" 
131          translationnode = ourdom.getFirstElementByTagName(message, "translation") 
132          return translationnode.getAttribute("type") 
 133   
140   
145   
147          """clean up the document if required""" 
148          if hasattr(self, "document"): 
149              self.document.unlink() 
  150