1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert Gettext PO localization files to an OpenOffice.org (SDF) localization file
24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2oo for examples and
26 usage instructions
27 """
28
29 import os
30 import sys
31 import time
32
33 from translate.storage import oo
34 from translate.storage import factory
35 from translate.filters import pofilter
36 from translate.filters import checks
37 from translate.filters import autocorrect
38
39
41
42 - def __init__(self, templatefile, languages=None, timestamp=None, includefuzzy=False, long_keys=False, filteraction="exclude"):
43 """construct a reoo converter for the specified languages (timestamp=0 means leave unchanged)"""
44
45 self.long_keys = long_keys
46 self.readoo(templatefile)
47 self.languages = languages
48 self.filteraction = filteraction
49 if timestamp is None:
50 self.timestamp = time.strptime("2002-02-02 02:02:02", "%Y-%m-%d %H:%M:%S")
51 else:
52 self.timestamp = timestamp
53 if self.timestamp:
54 self.timestamp_str = time.strftime("%Y-%m-%d %H:%M:%S", self.timestamp)
55 else:
56 self.timestamp_str = None
57 self.includefuzzy = includefuzzy
58
60 """makes an index of the oo keys that are used in the source file"""
61 self.index = {}
62 for ookey, theoo in self.o.ookeys.iteritems():
63 sourcekey = oo.makekey(ookey, self.long_keys)
64 self.index[sourcekey] = theoo
65
72
74
75 locations = unit.getlocations()
76
77
78 for location in locations:
79 subkeypos = location.rfind('.')
80 subkey = location[subkeypos+1:]
81 key = location[:subkeypos]
82
83 key = key.replace(':', '#')
84
85 key = key.replace('\\', '/')
86 key = oo.normalizefilename(key)
87 if key in self.index:
88
89 theoo = self.index[key]
90 self.applytranslation(key, subkey, theoo, unit)
91 else:
92 print >> sys.stderr, "couldn't find key %s from po in %d keys" % (key, len(self.index))
93 try:
94 sourceunitlines = str(unit)
95 if isinstance(sourceunitlines, unicode):
96 sourceunitlines = sourceunitlines.encode("utf-8")
97 print >> sys.stderr, sourceunitlines
98 except:
99 print >> sys.stderr, "error outputting source unit %r" % (str(unit),)
100
102 """applies the translation from the source unit to the oo unit"""
103 if not self.includefuzzy and unit.isfuzzy():
104 return
105 makecopy = False
106 if self.languages is None:
107 part1 = theoo.lines[0]
108 if len(theoo.lines) > 1:
109 part2 = theoo.lines[1]
110 else:
111 makecopy = True
112 else:
113 part1 = theoo.languages[self.languages[0]]
114 if self.languages[1] in theoo.languages:
115 part2 = theoo.languages[self.languages[1]]
116 else:
117 makecopy = True
118 if makecopy:
119 part2 = oo.ooline(part1.getparts())
120 unquotedid = unit.source
121 unquotedstr = unit.target
122
123 if len(unquotedstr) == 0:
124 return
125 if isinstance(unquotedstr, unicode):
126 unquotedstr = unquotedstr.encode("UTF-8")
127
128 if len(unquotedstr) > 0:
129 setattr(part2, subkey, unquotedstr)
130
131 if self.timestamp_str:
132 part2.timestamp = self.timestamp_str
133 if self.languages:
134 part2.languageid = self.languages[1]
135 if makecopy:
136 theoo.addline(part2)
137
139 self.p = sourcestore
140
141 for unit in self.p.units:
142
143 if filter.validelement(unit, self.p.filename, self.filteraction):
144 self.handleunit(unit)
145
146 return self.o
147
148
150 import stat
151 return time.localtime(os.stat(filename)[stat.ST_MTIME])
152
153
155
157 """Returns whether or not to use unit in conversion. (filename is just for error reporting)"""
158 if filteraction == "none":
159 return True
160 filterresult = self.filterunit(unit)
161 if filterresult:
162 if filterresult != autocorrect:
163 for filtername, filtermessage in filterresult.iteritems():
164 location = unit.getlocations()[0].encode('utf-8')
165 if filtername in self.options.error:
166 print >> sys.stderr, "Error at %s::%s: %s" % (filename, location, filtermessage)
167 return not filteraction in ["exclude-all", "exclude-serious"]
168 if filtername in self.options.warning or self.options.alwayswarn:
169 print >> sys.stderr, "Warning at %s::%s: %s" % (filename, location, filtermessage)
170 return not filteraction in ["exclude-all"]
171 return True
172
173
187
188 options = oofilteroptions()
189 filter = oocheckfilter(options, [checks.OpenOfficeChecker, checks.StandardUnitChecker], checks.openofficeconfig)
190
191
192 -def convertoo(inputfile, outputfile, templatefile, sourcelanguage=None, targetlanguage=None, timestamp=None, includefuzzy=False, multifilestyle="single", filteraction=None):
211
212
213 -def main(argv=None):
214 from translate.convert import convert
215 formats = {("po", "oo"): ("oo", convertoo), ("xlf", "oo"): ("oo", convertoo), ("po", "sdf"): ("sdf", convertoo)}
216
217 archiveformats = {(None, "output"): oo.oomultifile, (None, "template"): oo.oomultifile}
218 parser = convert.ArchiveConvertOptionParser(formats, usetemplates=True, description=__doc__, archiveformats=archiveformats)
219 parser.add_option("-l", "--language", dest="targetlanguage", default=None,
220 help="set target language code (e.g. af-ZA) [required]", metavar="LANG")
221 parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
222 help="set source language code (default en-US)", metavar="LANG")
223 parser.add_option("-T", "--keeptimestamp", dest="timestamp", default=None, action="store_const", const=0,
224 help="don't change the timestamps of the strings")
225 parser.add_option("", "--nonrecursiveoutput", dest="allowrecursiveoutput", default=True, action="store_false", help="don't treat the output oo as a recursive store")
226 parser.add_option("", "--nonrecursivetemplate", dest="allowrecursivetemplate", default=True, action="store_false", help="don't treat the template oo as a recursive store")
227 parser.add_option("", "--filteraction", dest="filteraction", default="none", metavar="ACTION",
228 help="action on pofilter failure: none (default), warn, exclude-serious, exclude-all")
229 parser.add_fuzzy_option()
230 parser.add_multifile_option()
231 parser.passthrough.append("sourcelanguage")
232 parser.passthrough.append("targetlanguage")
233 parser.passthrough.append("timestamp")
234 parser.passthrough.append("filteraction")
235 parser.run(argv)
236
237
238 if __name__ == '__main__':
239 main()
240