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

Source Code for Module translate.convert.po2tmx

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2005, 2006 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 TMX (Translation Memory eXchange) file 
 24   
 25  see: http://translate.sourceforge.net/wiki/toolkit/po2tmx for examples and  
 26  usage instructions 
 27  """ 
 28   
 29  from translate.storage import po 
 30  from translate.storage import tmx 
 31  from translate.convert import convert 
 32  from translate.misc import wStringIO 
 33  import os 
 34   
35 -class po2tmx:
36 - def convertfile(self, inputfile, sourcelanguage='en', targetlanguage=None):
37 """converts a .po file to TMX file""" 38 # TODO: This seems to not be used... remove it 39 inputstore = inputfile 40 for inunit in inputstore.units: 41 if inunit.isheader() or inunit.isblank() or not inunit.istranslated() or inunit.isfuzzy(): 42 continue 43 source = inunit.source 44 translation = inunit.target 45 # TODO place source location in comments 46 tmxfile.addtranslation(source, sourcelanguage, translation, targetlanguage) 47 return str(tmxfile)
48
49 - def convertfiles(self, inputfile, tmxfile, sourcelanguage='en', targetlanguage=None):
50 """converts a .po file (possibly many) to TMX file""" 51 inputstore = po.pofile(inputfile) 52 for inunit in inputstore.units: 53 if inunit.isheader() or inunit.isblank() or not inunit.istranslated() or inunit.isfuzzy(): 54 continue 55 source = inunit.source 56 translation = inunit.target 57 # TODO place source location in comments 58 tmxfile.addtranslation(source, sourcelanguage, translation, targetlanguage)
59
60 -def convertpo(inputfile, outputfile, templatefile, sourcelanguage='en', targetlanguage=None):
61 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 62 convertor = po2tmx() 63 convertor.convertfiles(inputfile, outputfile.tmxfile, sourcelanguage, targetlanguage) 64 return 1
65
66 -class tmxmultifile:
67 - def __init__(self, filename, mode=None):
68 """initialises tmxmultifile from a seekable inputfile or writable outputfile""" 69 self.filename = filename 70 if mode is None: 71 if os.path.exists(filename): 72 mode = 'r' 73 else: 74 mode = 'w' 75 self.mode = mode 76 # self.multifilestyle = multifilestyle 77 self.multifilename = os.path.splitext(filename)[0] 78 # self.multifile = open(filename, mode) 79 self.tmxfile = tmx.tmxfile()
80
81 - def openoutputfile(self, subfile):
82 """returns a pseudo-file object for the given subfile""" 83 def onclose(contents): 84 pass
85 outputfile = wStringIO.CatchStringOutput(onclose) 86 outputfile.filename = subfile 87 outputfile.tmxfile = self.tmxfile 88 return outputfile
89 90
91 -class TmxOptionParser(convert.ArchiveConvertOptionParser):
92 - def recursiveprocess(self, options):
93 if not options.targetlanguage: 94 raise ValueError("You must specify the target language") 95 super(TmxOptionParser, self).recursiveprocess(options) 96 self.output = open(options.output, 'w') 97 options.outputarchive.tmxfile.setsourcelanguage(options.sourcelanguage) 98 self.output.write(str(options.outputarchive.tmxfile))
99
100 -def main(argv=None):
101 formats = {"po": ("tmx", convertpo), ("po", "tmx"): ("tmx", convertpo)} 102 archiveformats = {(None, "output"): tmxmultifile, (None, "template"): tmxmultifile} 103 parser = TmxOptionParser(formats, usepots=False, usetemplates=False, description=__doc__, archiveformats=archiveformats) 104 parser.add_option("-l", "--language", dest="targetlanguage", default=None, 105 help="set target language code (e.g. af-ZA) [required]", metavar="LANG") 106 parser.add_option("", "--source-language", dest="sourcelanguage", default='en', 107 help="set source language code (default: en)", metavar="LANG") 108 parser.passthrough.append("sourcelanguage") 109 parser.passthrough.append("targetlanguage") 110 parser.run(argv)
111 112 113 if __name__ == '__main__': 114 main() 115