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