2 # -*- coding: utf-8 -*-
4 # Copyright 2005-2007 Zuza Software Foundation
6 # This file is part of translate.
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.
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.
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
23 """module for parsing TMX translation memeory files"""
25 from translate
.storage
import lisa
26 from lxml
import etree
28 from translate
import __version__
30 class tmxunit(lisa
.LISAunit
):
31 """A single unit in the TMX file."""
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
)
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()
53 def istranslatable(self
):
54 return bool(self
.source
)
56 def addnote(self
, text
, origin
=None):
57 """Add a note specifically in a "note" tag.
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()
65 def getnotelist(self
, origin
=None):
66 """Private method that returns the text from notes.
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
]
74 def getnotes(self
, origin
=None):
75 return '\n'.join(self
.getnotelist(origin
=origin
))
77 def removenotes(self
):
78 """Remove all the translator notes."""
79 notes
= self
.xmlelement
.findall(".//%s" % self
.namespaced("note"))
81 self
.xmlelement
.remove(note
)
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")
90 """Get all error messages."""
91 #TODO: consider factoring out: some duplication between XLIFF and TMX
92 notelist
= self
.getnotelist(origin
="pofilter")
95 errorname
, errortext
= note
.split(': ')
96 errordict
[errorname
] = errortext
100 """Make a copy of the translation unit.
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
))
110 class tmxfile(lisa
.LISAfile
):
111 """Class representing a TMX file store."""
115 XMLskeleton
= '''<?xml version="1.0" encoding="utf-8"?>
116 <!DOCTYPE tmx SYSTEM "tmx14.dtd">
123 headernode
= self
.document
.find("//%s" % self
.namespaced("header"))
124 headernode
.set("creationtool", "Translate Toolkit - po2tmx")
125 headernode
.set("creationtoolversion", __version__
.ver
)
126 headernode
.set("segtype", "sentence")
127 headernode
.set("o-tmf", "UTF-8")
128 headernode
.set("adminlang", "en")
129 #TODO: consider adminlang. Used for notes, etc. Possibly same as targetlanguage
130 headernode
.set("srclang", self
.sourcelanguage
)
131 headernode
.set("datatype", "PlainText")
132 #headernode.set("creationdate", "YYYYMMDDTHHMMSSZ"
133 #headernode.set("creationid", "CodeSyntax"
135 def addtranslation(self
, source
, srclang
, translation
, translang
):
136 """addtranslation method for testing old unit tests"""
137 unit
= self
.addsourceunit(source
)
138 unit
.target
= translation
139 tuvs
= unit
.xmlelement
.findall('.//%s' % self
.namespaced('tuv'))
140 lisa
.setXMLlang(tuvs
[0], srclang
)
141 lisa
.setXMLlang(tuvs
[1], translang
)
143 def translate(self
, sourcetext
, sourcelang
=None, targetlang
=None):
144 """method to test old unit tests"""
145 return getattr(self
.findunit(sourcetext
), "target", None)