1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """convert .rc files to Gettext PO localization files"""
22
23 import sys
24 from translate.storage import po
25 from translate.storage import rc
26
28 """Convert a .rc file to a .po file for handling the translation."""
30 self.charset = charset
31
33 """converts a .rc file to a .po file..."""
34 output_store = po.pofile()
35 output_header = output_store.makeheader(charset="UTF-8", encoding="8bit")
36 output_header.addnote("extracted from %s" % input_store.filename, "developer")
37 output_store.addunit(output_header)
38 for input_unit in input_store.units:
39 output_unit = self.convert_unit(input_unit, "developer")
40 if output_unit is not None:
41 output_store.addunit(output_unit)
42 output_store.removeduplicates(duplicatestyle)
43 return output_store
44
45 - def merge_store(self, template_store, input_store, blankmsgstr=False, duplicatestyle="msgctxt"):
46 """converts two .rc files to a .po file..."""
47 output_store = po.pofile()
48 output_header = output_store.makeheader(charset="UTF-8", encoding="8bit")
49 output_header.addnote("extracted from %s, %s" % (template_store.filename, input_store.filename), "developer")
50 output_store.addunit(output_header)
51 input_store.makeindex()
52 for template_unit in template_store.units:
53 origpo = self.convert_unit(template_unit, "developer")
54
55 template_unit_name = "".join(template_unit.getlocations())
56 if template_unit_name in input_store.locationindex:
57 translatedrc = input_store.locationindex[template_unit_name]
58 translatedpo = self.convert_unit(translatedrc, "translator")
59 else:
60 translatedpo = None
61
62 if origpo is not None:
63 if translatedpo is not None and not blankmsgstr:
64 origpo.target = translatedpo.source
65 output_store.addunit(origpo)
66 elif translatedpo is not None:
67 print >> sys.stderr, "error converting original rc definition %s" % origrc.name
68 output_store.removeduplicates(duplicatestyle)
69 return output_store
70
72 """Converts a .rc unit to a .po unit. Returns None if empty
73 or not for translation."""
74 if input_unit is None:
75 return None
76
77 output_unit = po.pounit(encoding="UTF-8")
78 output_unit.addlocation("".join(input_unit.getlocations()))
79 output_unit.source = input_unit.source.decode(self.charset)
80 output_unit.target = ""
81 return output_unit
82
83 -def convertrc(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt", charset=None, lang=None, sublang=None):
84 """reads in input_file using rc, converts using rc2po, writes to output_file"""
85 input_store = rc.rcfile(input_file, lang, sublang)
86 convertor = rc2po(charset=charset)
87 if template_file is None:
88 output_store = convertor.convert_store(input_store, duplicatestyle=duplicatestyle)
89 else:
90 template_store = rc.rcfile(template_file, lang, sublang)
91 output_store = convertor.merge_store(template_store, input_store, blankmsgstr=pot, duplicatestyle=duplicatestyle)
92 if output_store.isempty():
93 return 0
94 output_file.write(str(output_store))
95 return 1
96
98 from translate.convert import convert
99 formats = {"rc": ("po", convertrc), ("rc", "rc"): ("po", convertrc),
100 "nls": ("po", convertrc), ("nls", "nls"): ("po", convertrc)}
101 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
102 DEFAULTCHARSET = "cp1252"
103 parser.add_option("", "--charset", dest="charset", default=DEFAULTCHARSET,
104 help="charset to use to decode the RC files (default: %s)" % DEFAULTCHARSET, metavar="CHARSET")
105 DEFAULTLANG = "LANG_ENGLISH"
106 parser.add_option("-l", "--lang", dest="lang", default=DEFAULTLANG,
107 help="LANG entry (default: %s)" % DEFAULTLANG, metavar="LANG")
108 DEFAULTSUBLANG = "SUBLANG_DEFAULT"
109 parser.add_option("", "--sublang", dest="sublang", default=DEFAULTSUBLANG,
110 help="SUBLANG entry (default: %s)" % DEFAULTSUBLANG, metavar="SUBLANG")
111 parser.add_duplicates_option()
112 parser.passthrough.append("pot")
113 parser.passthrough.append("charset")
114 parser.passthrough.append("lang")
115 parser.passthrough.append("sublang")
116 parser.run(argv)
117
118 if __name__ == '__main__':
119 main()
120