1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Convert Gettext PO localization files back to Windows Resource (.rc) files
22
23 See: http://translate.sourceforge.net/wiki/toolkit/po2rc for examples and
24 usage instructions.
25 """
26
27 from translate.storage import po
28 from translate.storage import rc
29
30
32
33 - def __init__(self, templatefile, charset="utf-8", lang=None, sublang=None):
34 self.templatefile = templatefile
35 self.templatestore = rc.rcfile(templatefile, encoding=charset)
36 self.inputdict = {}
37 self.charset = charset
38 self.lang = lang
39 self.sublang = sublang
40
42 self.makestoredict(inputstore, includefuzzy)
43 outputblocks = []
44 for block in self.templatestore.blocks:
45 outputblocks.append(self.convertblock(block))
46 if self.charset == "utf-8":
47 outputblocks.insert(0, "#pragma code_page(65001)\n")
48 outputblocks.append("#pragma code_page(default)")
49 return outputblocks
50
52 """ make a dictionary of the translations"""
53 for unit in store.units:
54 if includefuzzy or not unit.isfuzzy():
55 for location in unit.getlocations():
56 rcstring = unit.target
57 if len(rcstring.strip()) == 0:
58 rcstring = unit.source
59 self.inputdict[location] = rc.escape_to_rc(rcstring).encode(self.charset)
60
62 newblock = block
63 if isinstance(newblock, unicode):
64 newblock = newblock.encode('utf-8')
65 if newblock.startswith("LANGUAGE"):
66 return "LANGUAGE %s, %s" % (self.lang, self.sublang)
67 for unit in self.templatestore.units:
68 location = unit.getlocations()[0]
69 if location in self.inputdict:
70 if self.inputdict[location] != unit.match.groupdict()['value']:
71 newmatch = unit.match.group().replace(unit.match.groupdict()['value'],
72 self.inputdict[location])
73 newblock = newblock.replace(unit.match.group(), newmatch)
74 if isinstance(newblock, unicode):
75 newblock = newblock.encode(self.charset)
76 return newblock
77
78
79 -def convertrc(inputfile, outputfile, templatefile, includefuzzy=False,
80 charset=None, lang=None, sublang=None):
81 inputstore = po.pofile(inputfile)
82 if not lang:
83 raise ValueError("must specify a target language")
84 if templatefile is None:
85 raise ValueError("must have template file for rc files")
86
87 else:
88 convertor = rerc(templatefile, charset, lang, sublang)
89 outputrclines = convertor.convertstore(inputstore, includefuzzy)
90 outputfile.writelines(outputrclines)
91 return 1
92
93
95
96 from translate.convert import convert
97 formats = {("po", "rc"): ("rc", convertrc)}
98 parser = convert.ConvertOptionParser(formats, usetemplates=True,
99 description=__doc__)
100 defaultcharset = "utf-8"
101 parser.add_option("", "--charset", dest="charset", default=defaultcharset,
102 help="charset to use to decode the RC files (default: %s)" % defaultcharset,
103 metavar="CHARSET")
104 parser.add_option("-l", "--lang", dest="lang", default=None,
105 help="LANG entry", metavar="LANG")
106 defaultsublang = "SUBLANG_DEFAULT"
107 parser.add_option("", "--sublang", dest="sublang", default=defaultsublang,
108 help="SUBLANG entry (default: %s)" % defaultsublang, metavar="SUBLANG")
109 parser.passthrough.append("charset")
110 parser.passthrough.append("lang")
111 parser.passthrough.append("sublang")
112 parser.add_fuzzy_option()
113 parser.run(argv)
114
115 if __name__ == '__main__':
116 main()
117