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
32
34
35 - def convertunit(self, outputstore, inputunit, filename):
36 """creates a transunit node"""
37 source = inputunit.source
38 target = inputunit.target
39 if inputunit.isheader():
40 unit = outputstore.addheaderunit(target, filename)
41 else:
42 unit = outputstore.addsourceunit(source, filename, True)
43 unit.target = target
44
45
46 if target:
47 unit.markfuzzy(inputunit.isfuzzy())
48 else:
49 unit.markapproved(False)
50
51
52 for location in inputunit.getlocations():
53 unit.createcontextgroup("po-reference", self.contextlist(location), purpose="location")
54
55
56 comment = inputunit.getnotes("developer")
57 if comment:
58 unit.createcontextgroup("po-entry", [("x-po-autocomment", comment)], purpose="information")
59 unit.addnote(comment, origin="developer")
60
61
62
63
64
65 comment = inputunit.getnotes("translator")
66 if comment:
67 unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information")
68 unit.addnote(comment, origin="po-translator")
69
70 return unit
71
72 - def contextlist(self, location):
73 contexts = []
74 if ":" in location:
75 sourcefile, linenumber = location.split(":", 1)
76 else:
77 sourcefile, linenumber = location, None
78 contexts.append(("sourcefile", sourcefile))
79 if linenumber:
80 contexts.append(("linenumber", linenumber))
81 return contexts
82
83 - def convertstore(self, inputstore, templatefile=None, **kwargs):
84 """converts a .po file to .xlf format"""
85 if templatefile is None:
86 outputstore = poxliff.PoXliffFile(**kwargs)
87 else:
88 outputstore = poxliff.PoXliffFile(templatefile, **kwargs)
89 filename = inputstore.filename
90 for inputunit in inputstore.units:
91 if inputunit.isblank():
92 continue
93 transunitnode = self.convertunit(outputstore, inputunit, filename)
94 return str(outputstore)
95
96
97 -def convertpo(inputfile, outputfile, templatefile):
98 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
99 inputstore = po.pofile(inputfile)
100 if inputstore.isempty():
101 return 0
102 convertor = po2xliff()
103 outputstring = convertor.convertstore(inputstore, templatefile)
104 outputfile.write(outputstring)
105 return 1
106
107
108 -def main(argv=None):
109 from translate.convert import convert
110 formats = {"po": ("xlf", convertpo), ("po", "xlf"): ("xlf", convertpo)}
111 parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=True, description=__doc__)
112 parser.run(argv)
113
114
115 if __name__ == '__main__':
116 main()
117