for git v1.5.2 (and below): chdir to the directory of the target file before executin...
[translate_toolkit.git] / tools / pocompile.py
blob1fb915c15e7ae4f2e0764ed59fa99ba988617c29
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2005, 2006 Zuza Software Foundation
5 #
6 # This file is part of the translate-toolkit
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.
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.
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
22 """Compile XLIFF and Gettext PO localization files into Gettext MO (Machine Object) files
24 See: http://translate.sourceforge.net/wiki/toolkit/pocompile for examples and
25 usage instructions
26 """
28 from translate.storage import factory
29 from translate.storage import po
30 from translate.storage import mo
32 class POCompile:
34 def convertstore(self, inputfile, includefuzzy=False):
35 outputfile = mo.mofile()
36 for unit in inputfile.units:
37 if unit.istranslated() or (unit.isfuzzy() and includefuzzy and unit.target):
38 mounit = mo.mounit()
39 if unit.isheader():
40 mounit.source = ""
41 else:
42 mounit.source = unit.source
43 if hasattr(unit, "msgidcomments"):
44 mounit.source.strings[0] = po.unquotefrompo(unit.msgidcomments) + mounit.source.strings[0]
45 if hasattr(unit, "msgctxt"):
46 mounit.msgctxt = po.unquotefrompo(unit.msgctxt)
47 mounit.target = unit.target
48 outputfile.addunit(mounit)
49 return str(outputfile)
51 def convertmo(inputfile, outputfile, templatefile, includefuzzy=False):
52 """reads in a base class derived inputfile, converts using pocompile, writes to outputfile"""
53 # note that templatefile is not used, but it is required by the converter...
54 inputstore = factory.getobject(inputfile)
55 if inputstore.isempty():
56 return 0
57 convertor = POCompile()
58 outputmo = convertor.convertstore(inputstore, includefuzzy)
59 # We have to make sure that we write the files in binary mode, therefore we
60 # reopen the file accordingly
61 outputfile.close()
62 outputfile = open(outputfile.name, 'wb')
63 outputfile.write(outputmo)
64 return 1
66 def main():
67 from translate.convert import convert
68 formats = {"po":("mo", convertmo), "xlf":("mo", convertmo)}
69 parser = convert.ConvertOptionParser(formats, usepots=False, description=__doc__)
70 parser.add_fuzzy_option()
71 parser.run()
73 if __name__ == '__main__':
74 main()