1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """convert Java/Mozilla .properties files to Gettext PO localization files
23
24 See: http://translate.sourceforge.net/wiki/toolkit/prop2po for examples and
25 usage instructions
26 """
27
28 import sys
29 from translate.storage import po
30 from translate.storage import properties
31
33 """convert a .properties file to a .po file for handling the translation..."""
34 - def convertstore(self, thepropfile, personality="java", duplicatestyle="msgctxt"):
35 """converts a .properties file to a .po file..."""
36 self.personality = personality
37 thetargetfile = po.pofile()
38 if self.personality == "mozilla" or self.personality == "skype":
39 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit", x_accelerator_marker="&")
40 else:
41 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
42 targetheader.addnote("extracted from %s" % thepropfile.filename, "developer")
43
44 appendedheader = False
45 waitingcomments = []
46 for propunit in thepropfile.units:
47 pounit = self.convertunit(propunit, "developer")
48 if pounit is None:
49 waitingcomments.extend(propunit.comments)
50
51 if pounit is "discard":
52 continue
53 if not appendedheader:
54 if propunit.isblank():
55 targetheader.addnote("\n".join(waitingcomments).rstrip(), "developer", position="prepend")
56 waitingcomments = []
57 pounit = None
58 appendedheader = True
59 if pounit is not None:
60 pounit.addnote("\n".join(waitingcomments).rstrip(), "developer", position="prepend")
61 waitingcomments = []
62 thetargetfile.addunit(pounit)
63 thetargetfile.removeduplicates(duplicatestyle)
64 return thetargetfile
65
66 - def mergestore(self, origpropfile, translatedpropfile, personality="java", blankmsgstr=False, duplicatestyle="msgctxt"):
67 """converts two .properties files to a .po file..."""
68 self.personality = personality
69 thetargetfile = po.pofile()
70 if self.personality == "mozilla" or self.personality == "skype":
71 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit", x_accelerator_marker="&")
72 else:
73 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
74 targetheader.addnote("extracted from %s, %s" % (origpropfile.filename, translatedpropfile.filename), "developer")
75 translatedpropfile.makeindex()
76
77 appendedheader = False
78 waitingcomments = []
79
80 for origprop in origpropfile.units:
81 origpo = self.convertunit(origprop, "developer")
82 if origpo is None:
83 waitingcomments.extend(origprop.comments)
84
85 if origpo is "discard":
86 continue
87
88 if not appendedheader:
89 if origprop.isblank():
90 targetheader.addnote(u"".join(waitingcomments).rstrip(), "developer", position="prepend")
91 waitingcomments = []
92 origpo = None
93 appendedheader = True
94
95 if origprop.name in translatedpropfile.locationindex:
96 translatedprop = translatedpropfile.locationindex[origprop.name]
97
98 translatedpo = self.convertunit(translatedprop, "translator")
99 if translatedpo is "discard":
100 continue
101 else:
102 translatedpo = None
103
104 if origpo is not None:
105 if translatedpo is not None and not blankmsgstr:
106 origpo.target = translatedpo.source
107 origpo.addnote(u"".join(waitingcomments).rstrip(), "developer", position="prepend")
108 waitingcomments = []
109 thetargetfile.addunit(origpo)
110 elif translatedpo is not None:
111 print >> sys.stderr, "error converting original properties definition %s" % origprop.name
112 thetargetfile.removeduplicates(duplicatestyle)
113 return thetargetfile
114
134
135 -def convertmozillaprop(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
136 """Mozilla specific convertor function"""
137 return convertprop(inputfile, outputfile, templatefile, personality="mozilla", pot=pot, duplicatestyle=duplicatestyle)
138
139 -def convertprop(inputfile, outputfile, templatefile, personality="java", pot=False, duplicatestyle="msgctxt"):
140 """reads in inputfile using properties, converts using prop2po, writes to outputfile"""
141 inputstore = properties.propfile(inputfile, personality)
142 convertor = prop2po()
143 if templatefile is None:
144 outputstore = convertor.convertstore(inputstore, personality, duplicatestyle=duplicatestyle)
145 else:
146 templatestore = properties.propfile(templatefile, personality)
147 outputstore = convertor.mergestore(templatestore, inputstore, personality, blankmsgstr=pot, duplicatestyle=duplicatestyle)
148 if outputstore.isempty():
149 return 0
150 outputfile.write(str(outputstore))
151 return 1
152
153 -def main(argv=None):
154 from translate.convert import convert
155 formats = {"properties": ("po", convertprop),
156 ("properties", "properties"): ("po", convertprop),
157 "lang": ("po", convertprop),
158 ("lang", "lang"): ("po", convertprop),}
159 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
160 parser.add_option("", "--personality", dest="personality", default="java", type="choice",
161 choices=["java", "mozilla", "skype"],
162 help="set the input behaviour: java (default), mozilla, skype", metavar="TYPE")
163 parser.add_duplicates_option()
164 parser.passthrough.append("pot")
165 parser.passthrough.append("personality")
166 parser.run(argv)
167
168 if __name__ == '__main__':
169 main()
170