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

Source Code for Module translate.convert.xliff2po

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2002-2009 Zuza Software Foundation 
  5  # 
  6  # This file is part of the Translate Toolkit. 
  7  # 
  8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>. 
 20   
 21  """Convert XLIFF localization files to Gettext PO localization files. 
 22   
 23  see: http://translate.sourceforge.net/wiki/toolkit/xliff2po for examples and 
 24  usage instructions. 
 25  """ 
 26   
 27  from translate.storage import po 
 28  from translate.storage import xliff 
 29  from translate.misc import wStringIO 
 30   
31 -class xliff2po:
32 - def converttransunit(self, transunit):
33 """makes a pounit from the given transunit""" 34 thepo = po.pounit() 35 36 #Header 37 if transunit.getrestype() == "x-gettext-domain-header": 38 thepo.source = "" 39 else: 40 thepo.source = transunit.source 41 thepo.target = transunit.target 42 43 #Location comments 44 locations = transunit.getlocations() 45 if locations: 46 thepo.addlocations(locations) 47 48 #NOTE: Supporting both <context> and <note> tags in xliff files for comments 49 #Translator comments 50 trancomments = transunit.getnotes("translator") 51 if trancomments: 52 thepo.addnote(trancomments, origin="translator") 53 54 #Automatic and Developer comments 55 autocomments = transunit.getnotes("developer") 56 if autocomments: 57 thepo.addnote(autocomments, origin="developer") 58 59 #See 5.6.1 of the spec. We should not check fuzzyness, but approved attribute 60 if transunit.isfuzzy(): 61 thepo.markfuzzy(True) 62 63 return thepo
64
65 - def convertstore(self, inputfile):
66 """Converts a .xliff file to .po format""" 67 # XXX: The inputfile is converted to string because Pootle supplies 68 # XXX: a PootleFile object as input which cannot be sent to PoXliffFile. 69 # XXX: The better way would be to have a consistent conversion API. 70 if not isinstance(inputfile, (file, wStringIO.StringIO)): 71 inputfile = str(inputfile) 72 XliffFile = xliff.xlifffile.parsestring(inputfile) 73 thetargetfile = po.pofile() 74 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit") 75 # TODO: support multiple files 76 for transunit in XliffFile.units: 77 if transunit.isheader(): 78 thetargetfile.updateheader(add=True, **XliffFile.parseheader()) 79 if transunit.getnotes('translator'): 80 targetheader.addnote(transunit.getnotes('translator'), origin='translator', position='replace') 81 if transunit.getnotes('developer'): 82 targetheader.addnote(transunit.getnotes('developer'), origin='developer', position='replace') 83 targetheader.markfuzzy(transunit.isfuzzy()) 84 continue 85 thepo = self.converttransunit(transunit) 86 thetargetfile.addunit(thepo) 87 return thetargetfile
88
89 -def convertxliff(inputfile, outputfile, templates):
90 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 91 convertor = xliff2po() 92 outputstore = convertor.convertstore(inputfile) 93 if outputstore.isempty(): 94 return 0 95 outputfile.write(str(outputstore)) 96 return 1
97
98 -def main(argv=None):
99 from translate.convert import convert 100 formats = {"xlf":("po", convertxliff)} 101 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) 102 parser.run(argv)
103