1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """convert .rc files to Gettext PO localization files"""
23
24 import sys
25 from translate.storage import po
26 from translate.storage import rc
27
29 """convert a .rc file to a .po file for handling the translation..."""
31 self.charset = charset
32
33 - def convertstore(self, thercfile, duplicatestyle="msgctxt"):
45
46 - def mergestore(self, origrcfile, translatedrcfile, blankmsgstr=False, duplicatestyle="msgctxt"):
47 """converts two .rc files to a .po file..."""
48 thetargetfile = po.pofile()
49 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
50 targetheader.addnote("extracted from %s, %s" % (origrcfile.filename, translatedrcfile.filename), "developer")
51 thetargetfile.addunit(targetheader)
52 translatedrcfile.makeindex()
53 for origrc in origrcfile.units:
54 origpo = self.convertunit(origrc, "developer")
55
56 origrcname = "".join(origrc.getlocations())
57 if origrcname in translatedrcfile.locationindex:
58 translatedrc = translatedrcfile.locationindex[origrcname]
59 translatedpo = self.convertunit(translatedrc, "translator")
60 else:
61 translatedpo = None
62
63 if origpo is not None:
64 if translatedpo is not None and not blankmsgstr:
65 origpo.target = translatedpo.source
66 thetargetfile.addunit(origpo)
67 elif translatedpo is not None:
68 print >> sys.stderr, "error converting original rc definition %s" % origrc.name
69 thetargetfile.removeduplicates(duplicatestyle)
70 return thetargetfile
71
83
84 -def convertrc(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt", charset=None):
85 """reads in inputfile using rc, converts using rc2po, writes to outputfile"""
86 inputstore = rc.rcfile(inputfile)
87 convertor = rc2po(charset=charset)
88 if templatefile is None:
89 outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle)
90 else:
91 templatestore = rc.rcfile(templatefile)
92 outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle)
93 if outputstore.isempty():
94 return 0
95 outputfile.write(str(outputstore))
96 return 1
97
99 from translate.convert import convert
100 formats = {"rc": ("po", convertrc), ("rc", "rc"): ("po", convertrc),
101 "nls": ("po", convertrc), ("nls", "nls"): ("po", convertrc)}
102 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
103 defaultcharset="cp1252"
104 parser.add_option("", "--charset", dest="charset", default=defaultcharset,
105 help="charset to use to decode the RC files (default: %s)" % defaultcharset, metavar="CHARSET")
106 parser.add_duplicates_option()
107 parser.passthrough.append("pot")
108 parser.passthrough.append("charset")
109 parser.run(argv)
110
111 if __name__ == '__main__':
112 main()
113