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

Source Code for Module translate.convert.po2ini

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