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

Source Code for Module translate.convert.po2txt

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2004-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 plain text (.txt) files 
 24   
 25  see: http://translate.sourceforge.net/wiki/toolkit/po2txt for examples and 
 26  usage instructions 
 27  """ 
 28   
 29  try: 
 30      import textwrap 
 31  except ImportError: 
 32      textwrap = None 
 33   
 34  from translate.storage import factory 
 35   
 36   
37 -class po2txt:
38 """po2txt can take a po file and generate txt. best to give it a template file otherwise will just concat msgstrs""" 39
40 - def __init__(self, wrap=None):
41 self.wrap = wrap
42
43 - def wrapmessage(self, message):
44 """rewraps text as required""" 45 if self.wrap is None: 46 return message 47 return "\n".join([textwrap.fill(line, self.wrap, replace_whitespace=False) for line in message.split("\n")])
48
49 - def convertstore(self, inputstore, includefuzzy):
50 """converts a file to txt format""" 51 txtresult = "" 52 for unit in inputstore.units: 53 if unit.isheader(): 54 continue 55 if unit.istranslated() or (includefuzzy and unit.isfuzzy()): 56 txtresult += self.wrapmessage(unit.target) + "\n" + "\n" 57 else: 58 txtresult += self.wrapmessage(unit.source) + "\n" + "\n" 59 return txtresult.rstrip()
60
61 - def mergestore(self, inputstore, templatetext, includefuzzy):
62 """converts a file to txt format""" 63 txtresult = templatetext 64 # TODO: make a list of blocks of text and translate them individually 65 # rather than using replace 66 for unit in inputstore.units: 67 if unit.isheader(): 68 continue 69 if not unit.isfuzzy() or includefuzzy: 70 txtsource = unit.source 71 txttarget = self.wrapmessage(unit.target) 72 if unit.istranslated(): 73 txtresult = txtresult.replace(txtsource, txttarget) 74 return txtresult
75 76
77 -def converttxt(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False, encoding='utf-8'):
78 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 79 inputstore = factory.getobject(inputfile) 80 convertor = po2txt(wrap=wrap) 81 if templatefile is None: 82 outputstring = convertor.convertstore(inputstore, includefuzzy) 83 else: 84 templatestring = templatefile.read().decode(encoding) 85 outputstring = convertor.mergestore(inputstore, templatestring, includefuzzy) 86 outputfile.write(outputstring.encode('utf-8')) 87 return 1
88 89
90 -def main(argv=None):
91 from translate.convert import convert 92 from translate.misc import stdiotell 93 import sys 94 sys.stdout = stdiotell.StdIOWrapper(sys.stdout) 95 formats = {("po", "txt"): ("txt", converttxt), ("po"): ("txt", converttxt), ("xlf", "txt"): ("txt", converttxt), ("xlf"): ("txt", converttxt)} 96 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__) 97 parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string", 98 help="The encoding of the template file (default: UTF-8)") 99 parser.passthrough.append("encoding") 100 if textwrap is not None: 101 parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int", 102 help="set number of columns to wrap text at", metavar="WRAP") 103 parser.passthrough.append("wrap") 104 parser.add_fuzzy_option() 105 parser.run(argv)
106 107 108 if __name__ == '__main__': 109 main() 110