Package translate :: Package convert :: Module xliff2po |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.xliff2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-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  """Convert XLIFF localization files to Gettext PO localization files.
     22
     23  see: http://translate.sourceforge.net/wiki/toolkit/xliff2po for examples and
     24  usage instructions.
     25  """
     26
     27  from translate.storage import po
     28  from translate.storage import xliff
     29  from translate.misc import wStringIO
     30
     31
     32 -class xliff2po:
     33
     34 -    def converttransunit(self, transunit):
     35          """makes a pounit from the given transunit"""
     36          thepo = po.pounit()
     37
     38          #Header
     39          if transunit.getrestype() == "x-gettext-domain-header":
     40              thepo.source = ""
     41          else:
     42              thepo.source = transunit.source
     43          thepo.target = transunit.target
     44
     45          #Location comments
     46          locations = transunit.getlocations()
     47          if locations:
     48              thepo.addlocations(locations)
     49
     50          #NOTE: Supporting both <context> and <note> tags in xliff files for comments
     51          #Translator comments
     52          trancomments = transunit.getnotes("translator")
     53          if trancomments:
     54              thepo.addnote(trancomments, origin="translator")
     55
     56          #Automatic and Developer comments
     57          autocomments = transunit.getnotes("developer")
     58          if autocomments:
     59              thepo.addnote(autocomments, origin="developer")
     60
     61          #See 5.6.1 of the spec. We should not check fuzzyness, but approved attribute
     62          if transunit.isfuzzy():
     63              thepo.markfuzzy(True)
     64
     65          return thepo
     66
     67 -    def convertstore(self, inputfile):
     68          """Converts a .xliff file to .po format"""
     69          # XXX: The inputfile is converted to string because Pootle supplies
     70          # XXX: a PootleFile object as input which cannot be sent to PoXliffFile.
     71          # XXX: The better way would be to have a consistent conversion API.
     72          if not isinstance(inputfile, (file, wStringIO.StringIO)):
     73              inputfile = str(inputfile)
     74          XliffFile = xliff.xlifffile.parsestring(inputfile)
     75          thetargetfile = po.pofile()
     76          targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
     77          # TODO: support multiple files
     78          for transunit in XliffFile.units:
     79              if transunit.isheader():
     80                  thetargetfile.updateheader(add=True, **XliffFile.parseheader())
     81                  if transunit.getnotes('translator'):
     82                      targetheader.addnote(transunit.getnotes('translator'), origin='translator', position='replace')
     83                  if transunit.getnotes('developer'):
     84                      targetheader.addnote(transunit.getnotes('developer'), origin='developer', position='replace')
     85                  targetheader.markfuzzy(transunit.isfuzzy())
     86                  continue
     87              thepo = self.converttransunit(transunit)
     88              thetargetfile.addunit(thepo)
     89          return thetargetfile
     90
     91
     92 -def convertxliff(inputfile, outputfile, templates):
     93      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
     94      convertor = xliff2po()
     95      outputstore = convertor.convertstore(inputfile)
     96      if outputstore.isempty():
     97          return 0
     98      outputfile.write(str(outputstore))
     99      return 1
    100
    101
    102 -def main(argv=None):
    103      from translate.convert import convert
    104      formats = {"xlf": ("po", convertxliff)}
    105      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    106      parser.run(argv)
    107