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 translate.storage import lisa
24 from lxml import etree
25
26 from translate import __version__
27
29 """A single unit in the TMX file."""
30 rootNode = "tu"
31 languageNode = "tuv"
32 textNode = "seg"
33
45
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
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
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
76
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
86 text = errorname
87 if errortext:
88 text += ': ' + errortext
89 self.addnote(text, origin="pofilter")
90
92 """Get all error messages."""
93
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
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
107 new_unit = self.__class__(None, empty=True)
108 new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement))
109 return new_unit
110
111
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
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
135 headernode.set("srclang", self.sourcelanguage)
136 headernode.set("datatype", "PlainText")
137
138
139
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