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

Source Code for Module translate.convert.po2sub

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  #  
 4  # Copyright 2008-2009 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 subtitle files""" 
24   
25  from translate.storage import factory 
26  from translate.storage import subtitles 
27   
28 -class resub:
29 - def __init__(self, templatefile):
30 self.templatefile = templatefile 31 self.templatestore = subtitles.SubtitleFile(templatefile) 32 self._inputdict = {}
33
34 - def convertstore(self, inputstore, includefuzzy=False):
35 self._makestoredict(inputstore, includefuzzy) 36 for unit in self.templatestore.units: 37 for location in unit.getlocations(): 38 if location in self._inputdict: 39 unit.target = self._inputdict[location] 40 else: 41 unit.target = unit.source 42 return str(self.templatestore)
43
44 - def _makestoredict(self, store, includefuzzy=False):
45 # make a dictionary of the translations 46 for unit in store.units: 47 if includefuzzy or not unit.isfuzzy(): 48 # there may be more than one entity due to msguniq merge 49 for location in unit.getlocations(): 50 substring = unit.target 51 if len(substring.strip()) == 0: 52 substring = unit.source 53 self._inputdict[location] = substring
54
55 -def convertsub(inputfile, outputfile, templatefile, includefuzzy=False):
56 inputstore = factory.getobject(inputfile) 57 if templatefile is None: 58 raise ValueError("must have template file for subtitle files") 59 else: 60 convertor = resub(templatefile) 61 outputstring = convertor.convertstore(inputstore, includefuzzy) 62 outputfile.write(outputstring) 63 return 1
64
65 -def main(argv=None):
66 # handle command line options 67 from translate.convert import convert 68 formats = {("po", "srt"): ("srt", convertsub)} 69 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__) 70 parser.add_fuzzy_option() 71 parser.run(argv)
72 73 if __name__ == '__main__': 74 main() 75