1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert Gettext PO localization files to Qt Linguist (.ts) files
24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2ts for examples and
26 usage instructions
27 """
28
29 from translate.storage import po
30 from translate.storage import ts
31
32
34
35 - def convertstore(self, inputstore, templatefile=None, context=None):
36 """converts a .po file to .ts format (using a template .ts file if given)"""
37 if templatefile is None:
38 tsfile = ts.QtTsParser()
39 else:
40 tsfile = ts.QtTsParser(templatefile)
41 for inputunit in inputstore.units:
42 if inputunit.isheader() or inputunit.isblank():
43 continue
44 source = inputunit.source
45 translation = inputunit.target
46 comment = inputunit.getnotes("translator")
47 transtype = None
48 if not inputunit.istranslated():
49 transtype = "unfinished"
50 elif inputunit.getnotes("developer") == "(obsolete)":
51 transtype = "obsolete"
52 if isinstance(source, str):
53 source = source.decode("utf-8")
54 if isinstance(translation, str):
55 translation = translation.decode("utf-8")
56 for sourcelocation in inputunit.getlocations():
57 if context is None:
58 if "#" in sourcelocation:
59 contextname = sourcelocation[:sourcelocation.find("#")]
60 else:
61 contextname = sourcelocation
62 else:
63 contextname = context
64 tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)
65 return tsfile.getxml()
66
67
68 -def convertpo(inputfile, outputfile, templatefile, context):
69 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
70 inputstore = po.pofile(inputfile)
71 if inputstore.isempty():
72 return 0
73 convertor = po2ts()
74 outputstring = convertor.convertstore(inputstore, templatefile, context)
75 outputfile.write(outputstring)
76 return 1
77
78
80 from translate.convert import convert
81 formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)}
82 parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)
83 parser.add_option("-c", "--context", dest="context", default=None,
84 help="use supplied context instead of the one in the .po file comment")
85 parser.passthrough.append("context")
86 parser.run(argv)
87
88
89 if __name__ == '__main__':
90 main()
91