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