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
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()
64
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
74
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)
83
84
85 if __name__ == '__main__':
86 main()
87