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 + ': ' + errortext
87 self.addnote(text, origin="pofilter")
88
90 """Get all error messages."""
91
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
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
105 new_unit = self.__class__(None, empty=True)
106 new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement))
107 return new_unit
108
109
111 """Class representing a TMX file store."""
112 UnitClass = tmxunit
113 Name = _("TMX Translation Memory")
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
126 headernode = self.document.getroot().iterchildren(self.namespaced("header")).next()
127 headernode.set("creationtool", "Translate Toolkit - po2tmx")
128 headernode.set("creationtoolversion", __version__.sver)
129 headernode.set("segtype", "sentence")
130 headernode.set("o-tmf", "UTF-8")
131 headernode.set("adminlang", "en")
132
133 headernode.set("srclang", self.sourcelanguage)
134 headernode.set("datatype", "PlainText")
135
136
137
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