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

Source Code for Module translate.convert.po2xliff

  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 XLIFF localization files 
 24   
 25  see: http://translate.sourceforge.net/wiki/toolkit/po2xliff for examples and 
 26  usage instructions 
 27  """ 
 28   
 29  from translate.storage import po 
 30  from translate.storage import poxliff 
 31   
 32   
33 -class po2xliff:
34
35 - def convertunit(self, outputstore, inputunit, filename):
36 """creates a transunit node""" 37 source = inputunit.source 38 target = inputunit.target 39 if inputunit.isheader(): 40 unit = outputstore.addheaderunit(target, filename) 41 else: 42 unit = outputstore.addsourceunit(source, filename, True) 43 unit.target = target 44 #Explicitly marking the fuzzy state will ensure that normal (translated) 45 #units in the PO file end up as approved in the XLIFF file. 46 if target: 47 unit.markfuzzy(inputunit.isfuzzy()) 48 else: 49 unit.markapproved(False) 50 51 #Handle #: location comments 52 for location in inputunit.getlocations(): 53 unit.createcontextgroup("po-reference", self.contextlist(location), purpose="location") 54 55 #Handle #. automatic comments 56 comment = inputunit.getnotes("developer") 57 if comment: 58 unit.createcontextgroup("po-entry", [("x-po-autocomment", comment)], purpose="information") 59 unit.addnote(comment, origin="developer") 60 61 #TODO: x-format, etc. 62 63 64 #Handle # other comments 65 comment = inputunit.getnotes("translator") 66 if comment: 67 unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information") 68 unit.addnote(comment, origin="po-translator") 69 70 return unit
71
72 - def contextlist(self, location):
73 contexts = [] 74 if ":" in location: 75 sourcefile, linenumber = location.split(":", 1) 76 else: 77 sourcefile, linenumber = location, None 78 contexts.append(("sourcefile", sourcefile)) 79 if linenumber: 80 contexts.append(("linenumber", linenumber)) 81 return contexts
82
83 - def convertstore(self, inputstore, templatefile=None, **kwargs):
84 """converts a .po file to .xlf format""" 85 if templatefile is None: 86 outputstore = poxliff.PoXliffFile(**kwargs) 87 else: 88 outputstore = poxliff.PoXliffFile(templatefile, **kwargs) 89 filename = inputstore.filename 90 for inputunit in inputstore.units: 91 if inputunit.isblank(): 92 continue 93 transunitnode = self.convertunit(outputstore, inputunit, filename) 94 return str(outputstore)
95 96
97 -def convertpo(inputfile, outputfile, templatefile):
98 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 99 inputstore = po.pofile(inputfile) 100 if inputstore.isempty(): 101 return 0 102 convertor = po2xliff() 103 outputstring = convertor.convertstore(inputstore, templatefile) 104 outputfile.write(outputstring) 105 return 1
106 107
108 -def main(argv=None):
109 from translate.convert import convert 110 formats = {"po": ("xlf", convertpo), ("po", "xlf"): ("xlf", convertpo)} 111 parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=True, description=__doc__) 112 parser.run(argv)
113 114 115 if __name__ == '__main__': 116 main() 117