Package translate :: Package storage :: Module tmx
[hide private]
[frames] | no frames]

Source Code for Module translate.storage.tmx

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2005-2007 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  8  # translate is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # translate is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with translate; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21  # 
 22   
 23  """module for parsing TMX translation memeory files""" 
 24   
 25  from translate.storage import lisa 
 26  from lxml import etree 
 27   
 28  from translate import __version__ 
 29   
30 -class tmxunit(lisa.LISAunit):
31 """A single unit in the TMX file.""" 32 rootNode = "tu" 33 languageNode = "tuv" 34 textNode = "seg" 35
36 - def createlanguageNode(self, lang, text, purpose):
37 """returns a langset xml Element setup with given parameters""" 38 if isinstance(text, str): 39 text = text.decode("utf-8") 40 langset = etree.Element(self.languageNode) 41 lisa.setXMLlang(langset, lang) 42 seg = etree.SubElement(langset, self.textNode) 43 seg.text = text 44 return langset
45
46 - def getid(self):
47 """Returns the identifier for this unit. The optional tuid property is 48 used if available, otherwise we inherit .getid(). Note that the tuid 49 property is only mandated to be unique from TMX 2.0.""" 50 id = self.xmlelement.get("tuid", "") 51 return id or super(tmxunit, self).getid()
52
53 - def istranslatable(self):
54 return bool(self.source)
55
56 - def addnote(self, text, origin=None):
57 """Add a note specifically in a "note" tag. 58 59 The origin parameter is ignored""" 60 if isinstance(text, str): 61 text = text.decode("utf-8") 62 note = etree.SubElement(self.xmlelement, self.namespaced("note")) 63 note.text = text.strip()
64
65 - def getnotelist(self, origin=None):
66 """Private method that returns the text from notes. 67 68 The origin parameter is ignored..""" 69 note_nodes = self.xmlelement.findall(".//%s" % self.namespaced("note")) 70 note_list = [lisa.getText(note) for note in note_nodes] 71 72 return note_list
73
74 - def getnotes(self, origin=None):
75 return '\n'.join(self.getnotelist(origin=origin))
76
77 - def removenotes(self):
78 """Remove all the translator notes.""" 79 notes = self.xmlelement.findall(".//%s" % self.namespaced("note")) 80 for note in notes: 81 self.xmlelement.remove(note)
82
83 - def adderror(self, errorname, errortext):
84 """Adds an error message to this unit.""" 85 #TODO: consider factoring out: some duplication between XLIFF and TMX 86 text = errorname + ': ' + errortext 87 self.addnote(text, origin="pofilter")
88
89 - def geterrors(self):
90 """Get all error messages.""" 91 #TODO: consider factoring out: some duplication between XLIFF and TMX 92 notelist = self.getnotelist(origin="pofilter") 93 errordict = {} 94 for note in notelist: 95 errorname, errortext = note.split(': ') 96 errordict[errorname] = errortext 97 return errordict
98
99 - def copy(self):
100 """Make a copy of the translation unit. 101 102 We don't want to make a deep copy - this could duplicate the whole XML 103 tree. For now we just serialise and reparse the unit's XML.""" 104 #TODO: check performance 105 new_unit = self.__class__(None, empty=True) 106 new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement)) 107 return new_unit
108 109
110 -class tmxfile(lisa.LISAfile):
111 """Class representing a TMX file store.""" 112 UnitClass = tmxunit 113 Name = "TMX file" 114 Mimetypes = ["application/x-tmx"] 115 Extensions = ["tmx"] 116 rootNode = "tmx" 117 bodyNode = "body" 118 XMLskeleton = '''<?xml version="1.0" encoding="utf-8"?> 119 <!DOCTYPE tmx SYSTEM "tmx14.dtd"> 120 <tmx version="1.4"> 121 <header></header> 122 <body></body> 123 </tmx>''' 124
125 - def addheader(self):
126 headernode = self.document.find("//%s" % self.namespaced("header")) 127 headernode.set("creationtool", "Translate Toolkit - po2tmx") 128 headernode.set("creationtoolversion", __version__.ver) 129 headernode.set("segtype", "sentence") 130 headernode.set("o-tmf", "UTF-8") 131 headernode.set("adminlang", "en") 132 #TODO: consider adminlang. Used for notes, etc. Possibly same as targetlanguage 133 headernode.set("srclang", self.sourcelanguage) 134 headernode.set("datatype", "PlainText")
135 #headernode.set("creationdate", "YYYYMMDDTHHMMSSZ" 136 #headernode.set("creationid", "CodeSyntax" 137
138 - def addtranslation(self, source, srclang, translation, translang):
139 """addtranslation method for testing old unit tests""" 140 unit = self.addsourceunit(source) 141 unit.target = translation 142 tuvs = unit.xmlelement.findall('.//%s' % self.namespaced('tuv')) 143 lisa.setXMLlang(tuvs[0], srclang) 144 lisa.setXMLlang(tuvs[1], translang)
145
146 - def translate(self, sourcetext, sourcelang=None, targetlang=None):
147 """method to test old unit tests""" 148 return getattr(self.findunit(sourcetext), "target", None)
149