Package translate :: Package tools :: Module pocompile
[hide private]
[frames] | no frames]

Source Code for Module translate.tools.pocompile

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  # Copyright 2005, 2006,2010 Zuza Software Foundation 
 5  # 
 6  # This file is part of the translate-toolkit 
 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  """Compile XLIFF and Gettext PO localization files into Gettext MO (Machine Object) files 
23   
24  See: http://translate.sourceforge.net/wiki/toolkit/pocompile for examples and 
25  usage instructions 
26  """ 
27   
28  from translate.storage import factory 
29  from translate.storage import mo 
30  from translate.misc.multistring import multistring 
31   
32   
33 -def _do_msgidcomment(string):
34 return u"_: %s\n" % string
35 36
37 -class POCompile:
38
39 - def convertstore(self, inputfile, includefuzzy=False):
40 outputfile = mo.mofile() 41 for unit in inputfile.units: 42 if unit.istranslated() or (unit.isfuzzy() and includefuzzy and unit.target) or unit.isheader(): 43 mounit = mo.mounit() 44 if unit.isheader(): 45 mounit.source = "" 46 else: 47 mounit.source = unit.source 48 context = unit.getcontext() 49 if unit.msgidcomment: 50 if mounit.hasplural(): 51 mounit.source = multistring(_do_msgidcomment(unit.msgidcomment) + mounit.source, *mounit.source.strings[1:]) 52 else: 53 mounit.source = _do_msgidcomment(unit.msgidcomment) + mounit.source 54 elif context: 55 mounit.msgctxt = [context] 56 mounit.target = unit.target 57 outputfile.addunit(mounit) 58 return str(outputfile)
59 60
61 -def convertmo(inputfile, outputfile, templatefile, includefuzzy=False):
62 """reads in a base class derived inputfile, converts using pocompile, writes to outputfile""" 63 # note that templatefile is not used, but it is required by the converter... 64 inputstore = factory.getobject(inputfile) 65 if inputstore.isempty(): 66 return 0 67 convertor = POCompile() 68 outputmo = convertor.convertstore(inputstore, includefuzzy) 69 # We have to make sure that we write the files in binary mode, therefore we 70 # reopen the file accordingly 71 outputfile.close() 72 outputfile = open(outputfile.name, 'wb') 73 outputfile.write(outputmo) 74 return 1
75 76
77 -def main():
78 from translate.convert import convert 79 formats = {"po": ("mo", convertmo), "xlf": ("mo", convertmo)} 80 parser = convert.ConvertOptionParser(formats, usepots=False, description=__doc__) 81 parser.add_fuzzy_option() 82 parser.run()
83 84 85 if __name__ == '__main__': 86 main() 87