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

Source Code for Module translate.convert.ts2po

 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 Qt Linguist (.ts) files to Gettext PO localization files 
24   
25  See: http://translate.sourceforge.net/wiki/toolkit/ts2po for examples and  
26  usage instructions 
27  """ 
28   
29   
30  from translate.storage import po 
31  from translate.storage import ts 
32   
33 -class ts2po:
34 - def convertmessage(self, contextname, messagenum, source, target, msgcomments, transtype):
35 """makes a pounit from the given message""" 36 thepo = po.pounit(encoding="UTF-8") 37 thepo.addlocation("%s#%d" % (contextname, messagenum)) 38 thepo.source = source 39 thepo.target = target 40 if len(msgcomments) > 0: 41 thepo.addnote(msgcomments) 42 if transtype == "unfinished" and thepo.istranslated(): 43 thepo.markfuzzy() 44 if transtype == "obsolete": 45 # This should use the Gettext obsolete method but it would require quite a bit of work 46 thepo.addnote("(obsolete)", origin="developer") 47 # using the fact that -- quote -- "(this is nonsense)" 48 return thepo
49
50 - def convertfile(self, inputfile):
51 """converts a .ts file to .po format""" 52 tsfile = ts.QtTsParser(inputfile) 53 thetargetfile = po.pofile() 54 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit") 55 thetargetfile.addunit(targetheader) 56 for contextname, messages in tsfile.iteritems(): 57 messagenum = 0 58 for message in messages: 59 messagenum += 1 60 source = tsfile.getmessagesource(message) 61 translation = tsfile.getmessagetranslation(message) 62 comment = tsfile.getmessagecomment(message) 63 transtype = tsfile.getmessagetype(message) 64 thepo = self.convertmessage(contextname, messagenum, source, translation, comment, transtype) 65 thetargetfile.addunit(thepo) 66 return thetargetfile
67
68 -def convertts(inputfile, outputfile, templates):
69 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 70 convertor = ts2po() 71 outputstore = convertor.convertfile(inputfile) 72 if outputstore.isempty(): 73 return 0 74 outputfile.write(str(outputstore)) 75 return 1
76
77 -def main(argv=None):
78 from translate.convert import convert 79 formats = {"ts":("po",convertts)} 80 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) 81 parser.run(argv)
82