1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """convert .ini files to Gettext PO localization files"""
23
24 import sys
25 from translate.storage import po
26 from translate.storage import xliff
27 from translate.storage import ini
28
30 """convert a .ini file to a .po file for handling the translation..."""
31 - def convertstore(self, theinifile, duplicatestyle="msgctxt"):
43
44 - def mergestore(self, originifile, translatedinifile, blankmsgstr=False, duplicatestyle="msgctxt"):
45 """converts two .ini files to a .po file..."""
46 thetargetfile = po.pofile()
47 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
48 targetheader.addnote("extracted from %s, %s" % (originifile.filename, translatedinifile.filename), "developer")
49 thetargetfile.addunit(targetheader)
50 translatedinifile.makeindex()
51 for origini in originifile.units:
52 origpo = self.convertunit(origini, "developer")
53
54 origininame = "".join(origini.getlocations())
55 if origininame in translatedinifile.locationindex:
56 translatedini = translatedinifile.locationindex[origininame]
57 translatedpo = self.convertunit(translatedini, "translator")
58 else:
59 translatedpo = None
60
61 if origpo is not None:
62 if translatedpo is not None and not blankmsgstr:
63 origpo.target = translatedpo.source
64 thetargetfile.addunit(origpo)
65 elif translatedpo is not None:
66 print >> sys.stderr, "error converting original ini definition %s" % origini.name
67 thetargetfile.removeduplicates(duplicatestyle)
68 return thetargetfile
69
81
82 -def convertini(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
83 """reads in inputfile using ini, converts using ini2po, writes to outputfile"""
84 inputstore = ini.inifile(inputfile)
85 convertor = ini2po()
86 if templatefile is None:
87 outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle)
88 else:
89 templatestore = ini.inifile(templatefile)
90 outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle)
91 if outputstore.isempty():
92 return 0
93 outputfile.write(str(outputstore))
94 return 1
95
97 from translate.convert import convert
98 formats = {"ini": ("po", convertini), ("ini", "ini"): ("po", convertini)}
99 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
100 parser.add_duplicates_option()
101 parser.passthrough.append("pot")
102 parser.run(argv)
103
104 if __name__ == '__main__':
105 main()
106