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-2009 Zuza Software Foundation 
  5  # 
  6  # This file is part of the Translate Toolkit. 
  7  # 
  8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>. 
 20   
 21  """module for parsing TMX translation memeory files""" 
 22   
 23  from translate.storage import lisa 
 24  from lxml import etree 
 25   
 26  from translate import __version__ 
 27   
28 -class tmxunit(lisa.LISAunit):
29 """A single unit in the TMX file.""" 30 rootNode = "tu" 31 languageNode = "tuv" 32 textNode = "seg" 33
34 - def createlanguageNode(self, lang, text, purpose):
35 """returns a langset xml Element setup with given parameters""" 36 if isinstance(text, str): 37 text = text.decode("utf-8") 38 langset = etree.Element(self.languageNode) 39 lisa.setXMLlang(langset, lang) 40 seg = etree.SubElement(langset, self.textNode) 41 # implied by the standard: 42 # lisa.setXMLspace(seg, "preserve") 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, position="append"):
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.iterdescendants(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.iterdescendants(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 87 if errortext: 88 text += ': ' + errortext 89 self.addnote(text, origin="pofilter")
90
91 - def geterrors(self):
92 """Get all error messages.""" 93 #TODO: consider factoring out: some duplication between XLIFF and TMX 94 notelist = self.getnotelist(origin="pofilter") 95 errordict = {} 96 for note in notelist: 97 errorname, errortext = note.split(': ') 98 errordict[errorname] = errortext 99 return errordict
100
101 - def copy(self):
102 """Make a copy of the translation unit. 103 104 We don't want to make a deep copy - this could duplicate the whole XML 105 tree. For now we just serialise and reparse the unit's XML.""" 106 #TODO: check performance 107 new_unit = self.__class__(None, empty=True) 108 new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement)) 109 return new_unit
110 111
112 -class tmxfile(lisa.LISAfile):
113 """Class representing a TMX file store.""" 114 UnitClass = tmxunit 115 Name = _("TMX Translation Memory") 116 Mimetypes = ["application/x-tmx"] 117 Extensions = ["tmx"] 118 rootNode = "tmx" 119 bodyNode = "body" 120 XMLskeleton = '''<?xml version="1.0" encoding="utf-8"?> 121 <!DOCTYPE tmx SYSTEM "tmx14.dtd"> 122 <tmx version="1.4"> 123 <header></header> 124 <body></body> 125 </tmx>''' 126
127 - def addheader(self):
128 headernode = self.document.getroot().iterchildren(self.namespaced("header")).next() 129 headernode.set("creationtool", "Translate Toolkit - po2tmx") 130 headernode.set("creationtoolversion", __version__.sver) 131 headernode.set("segtype", "sentence") 132 headernode.set("o-tmf", "UTF-8") 133 headernode.set("adminlang", "en") 134 #TODO: consider adminlang. Used for notes, etc. Possibly same as targetlanguage 135 headernode.set("srclang", self.sourcelanguage) 136 headernode.set("datatype", "PlainText")
137 #headernode.set("creationdate", "YYYYMMDDTHHMMSSZ" 138 #headernode.set("creationid", "CodeSyntax" 139
140 - def addtranslation(self, source, srclang, translation, translang):
141 """addtranslation method for testing old unit tests""" 142 unit = self.addsourceunit(source) 143 unit.target = translation 144 tuvs = unit.xmlelement.iterdescendants(self.namespaced('tuv')) 145 lisa.setXMLlang(tuvs.next(), srclang) 146 lisa.setXMLlang(tuvs.next(), translang)
147
148 - def translate(self, sourcetext, sourcelang=None, targetlang=None):
149 """method to test old unit tests""" 150 return getattr(self.findunit(sourcetext), "target", None)
151