2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
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
29 from translate
.storage
import po
30 from translate
.storage
import ts
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()
38 tsfile
= ts
.QtTsParser(templatefile
)
39 for inputunit
in inputstore
.units
:
40 if inputunit
.isheader() or inputunit
.isblank():
42 source
= inputunit
.source
43 translation
= inputunit
.target
44 comment
= inputunit
.getnotes("translator")
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():
56 if "#" in sourcelocation
:
57 contextname
= sourcelocation
[:sourcelocation
.find("#")]
59 contextname
= sourcelocation
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():
71 outputstring
= convertor
.convertstore(inputstore
, templatefile
, context
)
72 outputfile
.write(outputstring
)
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")
85 if __name__
== '__main__':