translate.convert.po2wordfast

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  import os
     30
     31  from translate.storage import po
     32  from translate.storage import wordfast
     33  from translate.convert import convert
     34  from translate.misc import wStringIO
     35
     36
     37 -class po2wordfast:
     38
     39 -    def convertfiles(self, inputfile, wffile, sourcelanguage='en', targetlanguage=None):
     40          """converts a .po file (possibly many) to a Wordfast TM file"""
     41          inputstore = po.pofile(inputfile)
     42          for inunit in inputstore.units:
     43              if inunit.isheader() or inunit.isblank() or not inunit.istranslated():
     44                  continue
     45              source = inunit.source
     46              target = inunit.target
     47              newunit = wffile.addsourceunit(source)
     48              newunit.target = target
     49              newunit.targetlang = targetlanguage
     50
     51
     52 -def convertpo(inputfile, outputfile, templatefile, sourcelanguage='en', targetlanguage=None):
     53      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
     54      convertor = po2wordfast()
     55      outputfile.wffile.header.targetlang = targetlanguage
     56      convertor.convertfiles(inputfile, outputfile.wffile, sourcelanguage, targetlanguage)
     57      return 1
     58
     59
     60 -class wfmultifile:
     61
     62 -    def __init__(self, filename, mode=None):
     63          """initialises wfmultifile from a seekable inputfile or writable outputfile"""
     64          self.filename = filename
     65          if mode is None:
     66              if os.path.exists(filename):
     67                  mode = 'r'
     68              else:
     69                  mode = 'w'
     70          self.mode = mode
     71          self.multifilename = os.path.splitext(filename)[0]
     72          self.wffile = wordfast.WordfastTMFile()
     73
     74 -    def openoutputfile(self, subfile):
     75          """returns a pseudo-file object for the given subfile"""
     76
     77          def onclose(contents):
     78              pass
     79          outputfile = wStringIO.CatchStringOutput(onclose)
     80          outputfile.filename = subfile
     81          outputfile.wffile = self.wffile
     82          return outputfile
     83
     84
     85 -class WfOptionParser(convert.ArchiveConvertOptionParser):
     86
     87 -    def recursiveprocess(self, options):
     88          if not options.targetlanguage:
     89              raise ValueError("You must specify the target language")
     90          super(WfOptionParser, self).recursiveprocess(options)
     91          self.output = open(options.output, 'w')
     92          #options.outputarchive.wffile.setsourcelanguage(options.sourcelanguage)
     93          self.output.write(str(options.outputarchive.wffile))
     94
     95
     96 -def main(argv=None):
     97      formats = {"po": ("txt", convertpo), ("po", "txt"): ("txt", convertpo)}
     98      archiveformats = {(None, "output"): wfmultifile, (None, "template"): wfmultifile}
     99      parser = WfOptionParser(formats, usepots=False, usetemplates=False, description=__doc__, archiveformats=archiveformats)
    100      parser.add_option("-l", "--language", dest="targetlanguage", default=None,
    101                        help="set target language code (e.g. af-ZA) [required]", metavar="LANG")
    102      parser.add_option("", "--source-language", dest="sourcelanguage", default='en',
    103                        help="set source language code (default: en)", metavar="LANG")
    104      parser.passthrough.append("sourcelanguage")
    105      parser.passthrough.append("targetlanguage")
    106      parser.run(argv)
    107
    108
    109  if __name__ == '__main__':
    110      main()
    111