1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Convert JSON files to Gettext PO localization files"""
23
24 import sys
25
26 from translate.storage import po
27
28
30 """Convert a JSON file to a PO file"""
31
33 """Converts a JSON file to a PO file"""
34 output_store = po.pofile()
35 output_header = output_store.init_headers(charset="UTF-8",
36 encoding="8bit")
37 output_header.addnote("extracted from %s" % input_store.filename,
38 "developer")
39 for input_unit in input_store.units:
40 output_unit = self.convert_unit(input_unit, "developer")
41 if output_unit is not None:
42 output_store.addunit(output_unit)
43 output_store.removeduplicates(duplicatestyle)
44 return output_store
45
46 - def merge_store(self, template_store, input_store, blankmsgstr=False,
47 duplicatestyle="msgctxt"):
48 """Converts two JSON files to a PO file"""
49 output_store = po.pofile()
50 output_header = output_store.init_headers(charset="UTF-8",
51 encoding="8bit")
52 output_header.addnote("extracted from %s, %s" % (template_store.filename,
53 input_store.filename),
54 "developer")
55
56 input_store.makeindex()
57 for template_unit in template_store.units:
58 origpo = self.convert_unit(template_unit, "developer")
59
60 template_unit_name = "".join(template_unit.getlocations())
61 if template_unit_name in input_store.locationindex:
62 translatedjson = input_store.locationindex[template_unit_name]
63 translatedpo = self.convert_unit(translatedjson, "translator")
64 else:
65 translatedpo = None
66
67 if origpo is not None:
68 if translatedpo is not None and not blankmsgstr:
69 origpo.target = translatedpo.source
70 output_store.addunit(origpo)
71 elif translatedpo is not None:
72 print >> sys.stderr, "Error converting original JSON definition %s" % origpo.name
73 output_store.removeduplicates(duplicatestyle)
74 return output_store
75
77 """Converts a JSON unit to a PO unit
78
79 @return: None if empty or not for translation
80 """
81 if input_unit is None:
82 return None
83
84 output_unit = po.pounit(encoding="UTF-8")
85 output_unit.addlocation(input_unit.getid())
86 output_unit.source = input_unit.source
87 output_unit.target = ""
88 return output_unit
89
90
91 -def convertjson(input_file, output_file, template_file, pot=False,
92 duplicatestyle="msgctxt", dialect="default", filter=None):
93 """Reads in L{input_file} using jsonl10n, converts using L{json2po},
94 writes to L{output_file}"""
95 from translate.storage import jsonl10n
96 if filter is not None:
97 filter = filter.split(',')
98 input_store = jsonl10n.JsonFile(input_file, filter=filter)
99 convertor = json2po()
100 if template_file is None:
101 output_store = convertor.convert_store(input_store,
102 duplicatestyle=duplicatestyle)
103 else:
104 template_store = jsonl10n.JsonFile(template_file, dialect=dialect)
105 output_store = convertor.merge_store(template_store, input_store,
106 blankmsgstr=pot,
107 duplicatestyle=duplicatestyle)
108 if output_store.isempty():
109 return 0
110 output_file.write(str(output_store))
111 return 1
112
113
114 -def main(argv=None):
115 from translate.convert import convert
116 formats = {
117 "json": ("po", convertjson),
118 ("json", "json"): ("po", convertjson),
119 }
120 parser = convert.ConvertOptionParser(formats, usetemplates=True,
121 usepots=True, description=__doc__)
122 parser.add_option("", "--filter", dest="filter", default=None,
123 help="leaves to extract e.g. 'name,desc': (default: extract everything)",
124 metavar="FILTER")
125 parser.add_duplicates_option()
126 parser.passthrough.append("pot")
127 parser.passthrough.append("filter")
128 parser.run(argv)
129
130
131 if __name__ == '__main__':
132 main()
133