translate.storage.tmx

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 lxml import etree
     24
     25  from translate import __version__
     26  from translate.storage import lisa
     27
     28
     29 -class tmxunit(lisa.LISAunit):
     30      """A single unit in the TMX file."""
     31      rootNode = "tu"
     32      languageNode = "tuv"
     33      textNode = "seg"
     34
     35 -    def createlanguageNode(self, lang, text, purpose):
     36          """returns a langset xml Element setup with given parameters"""
     37          if isinstance(text, str):
     38              text = text.decode("utf-8")
     39          langset = etree.Element(self.languageNode)
     40          lisa.setXMLlang(langset, lang)
     41          seg = etree.SubElement(langset, self.textNode)
     42          # implied by the standard:
     43          # lisa.setXMLspace(seg, "preserve")
     44          seg.text = text
     45          return langset
     46
     47 -    def getid(self):
     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
     54 -    def istranslatable(self):
     55          return bool(self.source)
     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
     66 -    def getnotelist(self, origin=None):
     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
     75 -    def getnotes(self, origin=None):
     76          return '\n'.join(self.getnotelist(origin=origin))
     77
     78 -    def removenotes(self):
     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          #TODO: consider factoring out: some duplication between XLIFF and TMX
     87          text = errorname
     88          if errortext:
     89              text += ': ' + errortext
     90          self.addnote(text, origin="pofilter")
     91
     92 -    def geterrors(self):
     93          """Get all error messages."""
     94          #TODO: consider factoring out: some duplication between XLIFF and TMX
     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
    102 -    def copy(self):
    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          #TODO: check performance
    108          new_unit = self.__class__(None, empty=True)
    109          new_unit.xmlelement = etree.fromstring(etree.tostring(self.xmlelement))
    110          return new_unit
    111
    112
    113 -class tmxfile(lisa.LISAfile):
    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
    128 -    def addheader(self):
    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          #TODO: consider adminlang. Used for notes, etc. Possibly same as targetlanguage
    136          headernode.set("srclang", self.sourcelanguage)
    137          headernode.set("datatype", "PlainText")
    138          #headernode.set("creationdate", "YYYYMMDDTHHMMSSZ"
    139          #headernode.set("creationid", "CodeSyntax"
    140
    141 -    def addtranslation(self, source, srclang, translation, translang):
    142          """addtranslation method for testing old unit tests"""
    143          unit = self.addsourceunit(source)
    144          unit.target = translation
    145          tuvs = unit.xmlelement.iterdescendants(self.namespaced('tuv'))
    146          lisa.setXMLlang(tuvs.next(), srclang)
    147          lisa.setXMLlang(tuvs.next(), translang)
    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