1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """module for parsing TMX translation memeory files"""
22
23 from lxml import etree
24
25 from translate import __version__
26 from translate.storage import lisa
27
28
30 """A single unit in the TMX file."""
31 rootNode = "tu"
32 languageNode = "tuv"
33 textNode = "seg"
34
46
48 """Returns the identifier for this unit. The optional tuid property is
49 used if available, otherwise we inherit .getid(). Note that the tuid
50 property is only mandated to be unique from TMX 2.0."""
51 id = self.xmlelement.get("tuid", "")
52 return id or super(tmxunit, self).getid()
53
56
57 - def addnote(self, text, origin=None, position="append"):
58 """Add a note specifically in a "note" tag.
59
60 The origin parameter is ignored"""
61 if isinstance(text, str):
62 text = text.decode("utf-8")
63 note = etree.SubElement(self.xmlelement, self.namespaced("note"))
64 note.text = text.strip()
65
67 """Private method that returns the text from notes.
68
69 The origin parameter is ignored.."""
70 note_nodes = self.xmlelement.iterdescendants(self.namespaced("note"))
71 note_list = [lisa.getText(note) for note in note_nodes]
72
73 return note_list
74
77
79 """Remove all the translator notes."""
80 notes = self.xmlelement.iterdescendants(self.namespaced("note"))
81 for note in notes:
82 self.xmlelement.remove(note)
83
84 - def adderror(self, errorname, errortext):
85 """Adds an error message to this unit."""
86
87 text = errorname
88 if errortext:
89 text += ': ' + errortext
90 self.addnote(text, origin="pofilter")
91
93 """Get all error messages."""
94
95 notelist = self.getnotelist(origin="pofilter")
96 errordict = {}
97 for note in notelist:
98 errorname, errortext = note.split(': ')
99 errordict[errorname] = errortext
100 return errordict
101
103 """Make a copy of the translation unit.
104
105 We don't want to make a deep copy - this could duplicate the whole XML
106 tree. For now we just serialise and reparse the unit's XML."""
107
108 new_unit = self.__class__(None, empty=True)
109 new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement))
110 return new_unit
111
112
114 """Class representing a TMX file store."""
115 UnitClass = tmxunit
116 Name = _("TMX Translation Memory")
117 Mimetypes = ["application/x-tmx"]
118 Extensions = ["tmx"]
119 rootNode = "tmx"
120 bodyNode = "body"
121 XMLskeleton = '''<?xml version="1.0" encoding="utf-8"?>
122 <!DOCTYPE tmx SYSTEM "tmx14.dtd">
123 <tmx version="1.4">
124 <header></header>
125 <body></body>
126 </tmx>'''
127
129 headernode = self.document.getroot().iterchildren(self.namespaced("header")).next()
130 headernode.set("creationtool", "Translate Toolkit - po2tmx")
131 headernode.set("creationtoolversion", __version__.sver)
132 headernode.set("segtype", "sentence")
133 headernode.set("o-tmf", "UTF-8")
134 headernode.set("adminlang", "en")
135
136 headernode.set("srclang", self.sourcelanguage)
137 headernode.set("datatype", "PlainText")
138
139
140
148
149 - def translate(self, sourcetext, sourcelang=None, targetlang=None):
150 """method to test old unit tests"""
151 return getattr(self.findunit(sourcetext), "target", None)
152