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 XLIFF localization files
24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2xliff for examples and
26 usage instructions
27 """
28
29 from translate.storage import po
30 from translate.storage import poxliff
31
33 - def convertunit(self, outputstore, inputunit, filename):
34 """creates a transunit node"""
35 source = inputunit.source
36 target = inputunit.target
37 if inputunit.isheader():
38 unit = outputstore.addheaderunit(target, filename)
39 else:
40 unit = outputstore.addsourceunit(source, filename, True)
41 unit.target = target
42
43
44 if target:
45 unit.markfuzzy(inputunit.isfuzzy())
46 else:
47 unit.markapproved(False)
48
49
50 for location in inputunit.getlocations():
51 unit.createcontextgroup("po-reference", self.contextlist(location), purpose="location")
52
53
54 comment = inputunit.getnotes("developer")
55 if comment:
56 unit.createcontextgroup("po-entry", [("x-po-autocomment", comment)], purpose="information")
57 unit.addnote(comment, origin="developer")
58
59
60
61
62
63 comment = inputunit.getnotes("translator")
64 if comment:
65 unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information")
66 unit.addnote(comment, origin="po-translator")
67
68 return unit
69
70 - def contextlist(self, location):
71 contexts = []
72 if ":" in location:
73 sourcefile, linenumber = location.split(":", 1)
74 else:
75 sourcefile, linenumber = location, None
76 contexts.append(("sourcefile", sourcefile))
77 if linenumber:
78 contexts.append(("linenumber", linenumber))
79 return contexts
80
81 - def convertstore(self, inputstore, templatefile=None, **kwargs):
93
94 -def convertpo(inputfile, outputfile, templatefile):
95 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
96 inputstore = po.pofile(inputfile)
97 if inputstore.isempty():
98 return 0
99 convertor = po2xliff()
100 outputstring = convertor.convertstore(inputstore, templatefile)
101 outputfile.write(outputstring)
102 return 1
103
104 -def main(argv=None):
105 from translate.convert import convert
106 formats = {"po": ("xlf", convertpo), ("po", "xlf"): ("xlf", convertpo)}
107 parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=True, description=__doc__)
108 parser.run(argv)
109
110 if __name__ == '__main__':
111 main()
112