fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / po2ts.py
blob46160518bb9e1abac8e0ebcd7377ae6883555a06
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
5 #
6 # This file is part of translate.
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
23 """convert Gettext PO localization files to Qt Linguist (.ts) files
25 see: http://translate.sourceforge.net/wiki/toolkit/po2ts for examples and
26 usage instructions
27 """
29 from translate.storage import po
30 from translate.storage import ts
32 class po2ts:
33 def convertstore(self, inputstore, templatefile=None, context=None):
34 """converts a .po file to .ts format (using a template .ts file if given)"""
35 if templatefile is None:
36 tsfile = ts.QtTsParser()
37 else:
38 tsfile = ts.QtTsParser(templatefile)
39 for inputunit in inputstore.units:
40 if inputunit.isheader() or inputunit.isblank():
41 continue
42 source = inputunit.source
43 translation = inputunit.target
44 comment = inputunit.getnotes("translator")
45 transtype = None
46 if not inputunit.istranslated():
47 transtype = "unfinished"
48 elif inputunit.getnotes("developer") == "(obsolete)":
49 transtype = "obsolete"
50 if isinstance(source, str):
51 source = source.decode("utf-8")
52 if isinstance(translation, str):
53 translation = translation.decode("utf-8")
54 for sourcelocation in inputunit.getlocations():
55 if context is None:
56 if "#" in sourcelocation:
57 contextname = sourcelocation[:sourcelocation.find("#")]
58 else:
59 contextname = sourcelocation
60 else:
61 contextname = context
62 tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)
63 return tsfile.getxml()
65 def convertpo(inputfile, outputfile, templatefile, context):
66 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
67 inputstore = po.pofile(inputfile)
68 if inputstore.isempty():
69 return 0
70 convertor = po2ts()
71 outputstring = convertor.convertstore(inputstore, templatefile, context)
72 outputfile.write(outputstring)
73 return 1
75 def main(argv=None):
76 from translate.convert import convert
77 formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)}
78 parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)
79 parser.add_option("-c", "--context", dest="context", default=None,
80 help="use supplied context instead of the one in the .po file comment")
81 parser.passthrough.append("context")
82 parser.run(argv)
85 if __name__ == '__main__':
86 main()