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 -class xliff2po:
    32 -    def converttransunit(self, transunit):
    33          """makes a pounit from the given transunit"""
    34          thepo = po.pounit()
    35
    36          #Header
    37          if transunit.getrestype() == "x-gettext-domain-header":
    38              thepo.source = ""
    39          else:
    40              thepo.source = transunit.source
    41          thepo.target = transunit.target
    42
    43          #Location comments
    44          locations = transunit.getlocations()
    45          if locations:
    46              thepo.addlocations(locations)
    47
    48          #NOTE: Supporting both <context> and <note> tags in xliff files for comments
    49          #Translator comments
    50          trancomments = transunit.getnotes("translator")
    51          if trancomments:
    52              thepo.addnote(trancomments, origin="translator")
    53
    54          #Automatic and Developer comments
    55          autocomments = transunit.getnotes("developer")
    56          if autocomments:
    57              thepo.addnote(autocomments, origin="developer")
    58
    59          #See 5.6.1 of the spec. We should not check fuzzyness, but approved attribute
    60          if transunit.isfuzzy():
    61              thepo.markfuzzy(True)
    62
    63          return thepo
    64
    65 -    def convertstore(self, inputfile):
    66          """Converts a .xliff file to .po format"""
    67          # XXX: The inputfile is converted to string because Pootle supplies
    68          # XXX: a PootleFile object as input which cannot be sent to PoXliffFile.
    69          # XXX: The better way would be to have a consistent conversion API.
    70          if not isinstance(inputfile, (file, wStringIO.StringIO)):
    71              inputfile = str(inputfile)
    72          XliffFile = xliff.xlifffile.parsestring(inputfile)
    73          thetargetfile = po.pofile()
    74          targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
    75          # TODO: support multiple files
    76          for transunit in XliffFile.units:
    77              thepo = self.converttransunit(transunit)
    78              thetargetfile.addunit(thepo)
    79          return thetargetfile
    80
    81 -def convertxliff(inputfile, outputfile, templates):
    82      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    83      convertor = xliff2po()
    84      outputstore = convertor.convertstore(inputfile)
    85      if outputstore.isempty():
    86          return 0
    87      outputfile.write(str(outputstore))
    88      return 1
    89
    90 -def main(argv=None):
    91      from translate.convert import convert
    92      formats = {"xlf":("po", convertxliff)}
    93      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    94      parser.run(argv)
    95