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

# Source Code for Module translate.convert.po2wordfast

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2005-2007 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      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.
     12  #
     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.
     17  #
     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
     21  #
     22
     23  """convert Gettext PO localization files to a Wordfast translation memory file
     24
     25  see: http://translate.sourceforge.net/wiki/toolkit/po2wordfast for examples and
     26  usage instructions
     27  """
     28
     29  from translate.storage import po
     30  from translate.storage import wordfast
     31  from translate.convert import convert
     32  from translate.misc import wStringIO
     33  import os
     34
     35 -class po2wordfast:
     36 -    def convertfiles(self, inputfile, wffile, sourcelanguage='en', targetlanguage=None):
     37          """converts a .po file (possibly many) to a Wordfast TM file"""
     38          inputstore = po.pofile(inputfile)
     39          for inunit in inputstore.units:
     40              if inunit.isheader() or inunit.isblank() or not inunit.istranslated():
     41                  continue
     42              source = inunit.source
     43              target = inunit.target
     44              newunit = wffile.addsourceunit(source)
     45              newunit.target = target
     46              newunit.targetlang = targetlanguage
     47
     48 -def convertpo(inputfile, outputfile, templatefile, sourcelanguage='en', targetlanguage=None):
     49      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
     50      convertor = po2wordfast()
     51      outputfile.wffile.header.targetlang = targetlanguage
     52      convertor.convertfiles(inputfile, outputfile.wffile, sourcelanguage, targetlanguage)
     53      return 1
     54
     55 -class wfmultifile:
     56 -    def __init__(self, filename, mode=None):
     57          """initialises wfmultifile from a seekable inputfile or writable outputfile"""
     58          self.filename = filename
     59          if mode is None:
     60              if os.path.exists(filename):
     61                  mode = 'r'
     62              else:
     63                  mode = 'w'
     64          self.mode = mode
     65          self.multifilename = os.path.splitext(filename)[0]
     66          self.wffile = wordfast.WordfastTMFile()
     67
     68 -    def openoutputfile(self, subfile):
     69          """returns a pseudo-file object for the given subfile"""
     70          def onclose(contents):
     71              pass
     72          outputfile = wStringIO.CatchStringOutput(onclose)
     73          outputfile.filename = subfile
     74          outputfile.wffile = self.wffile
     75          return outputfile
     76
     77
     78 -class WfOptionParser(convert.ArchiveConvertOptionParser):
     79 -    def recursiveprocess(self, options):
     80          if not options.targetlanguage:
     81              raise ValueError("You must specify the target language")
     82          super(WfOptionParser, self).recursiveprocess(options)
     83          self.output = open(options.output, 'w')
     84          #options.outputarchive.wffile.setsourcelanguage(options.sourcelanguage)
     85          self.output.write(str(options.outputarchive.wffile))
     86
     87 -def main(argv=None):
     88      formats = {"po": ("txt", convertpo), ("po", "txt"): ("txt", convertpo)}
     89      archiveformats = {(None, "output"): wfmultifile, (None, "template"): wfmultifile}
     90      parser = WfOptionParser(formats, usepots=False, usetemplates=False, description=__doc__, archiveformats=archiveformats)
     91      parser.add_option("-l", "--language", dest="targetlanguage", default=None,
     92                        help="set target language code (e.g. af-ZA) [required]", metavar="LANG")
     93      parser.add_option("", "--source-language", dest="sourcelanguage", default='en',
     94                        help="set source language code (default: en)", metavar="LANG")
     95      parser.passthrough.append("sourcelanguage")
     96      parser.passthrough.append("targetlanguage")
     97      parser.run(argv)
     98
     99
    100  if __name__ == '__main__':
    101      main()
    102