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