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
140 for unit in sourcestore.units:
141
142 if filter.validelement(unit, sourcestore.filename, self.filteraction):
143 self.handleunit(unit)
144
145 return self.o
146
147
149 import stat
150 return time.localtime(os.stat(filename)[stat.ST_MTIME])
151
152
154
156 """Returns whether or not to use unit in conversion. (filename is just for error reporting)"""
157 if filteraction == "none":
158 return True
159 filterresult = self.filterunit(unit)
160 if filterresult:
161 if filterresult != autocorrect:
162 for filtername, filtermessage in filterresult.iteritems():
163 location = unit.getlocations()[0].encode('utf-8')
164 if filtername in self.options.error:
165 print >> sys.stderr, "Error at %s::%s: %s" % (filename, location, filtermessage)
166 return not filteraction in ["exclude-all", "exclude-serious"]
167 if filtername in self.options.warning or self.options.alwayswarn:
168 print >> sys.stderr, "Warning at %s::%s: %s" % (filename, location, filtermessage)
169 return not filteraction in ["exclude-all"]
170 return True
171
172
186
187 options = oofilteroptions()
188 filter = oocheckfilter(options, [checks.OpenOfficeChecker, checks.StandardUnitChecker], checks.openofficeconfig)
189
190
191 -def convertoo(inputfile, outputfile, templatefile, sourcelanguage=None, targetlanguage=None, timestamp=None, includefuzzy=False, multifilestyle="single", skip_source=False, filteraction=None):
210
211
212 -def main(argv=None):
213 from translate.convert import convert
214 formats = {("po", "oo"): ("oo", convertoo), ("xlf", "oo"): ("oo", convertoo), ("po", "sdf"): ("sdf", convertoo)}
215
216 archiveformats = {(None, "output"): oo.oomultifile, (None, "template"): oo.oomultifile}
217 parser = convert.ArchiveConvertOptionParser(formats, usetemplates=True, description=__doc__, archiveformats=archiveformats)
218 parser.add_option("-l", "--language", dest="targetlanguage", default=None,
219 help="set target language code (e.g. af-ZA) [required]", metavar="LANG")
220 parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
221 help="set source language code (default en-US)", metavar="LANG")
222 parser.add_option("-T", "--keeptimestamp", dest="timestamp", default=None, action="store_const", const=0,
223 help="don't change the timestamps of the strings")
224 parser.add_option("", "--nonrecursiveoutput", dest="allowrecursiveoutput", default=True, action="store_false", help="don't treat the output oo as a recursive store")
225 parser.add_option("", "--nonrecursivetemplate", dest="allowrecursivetemplate", default=True, action="store_false", help="don't treat the template oo as a recursive store")
226 parser.add_option("", "--skipsource", dest="skip_source", default=False, action="store_true", help="don't output the source language, but fallback to it where needed")
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("skip_source")
235 parser.passthrough.append("filteraction")
236 parser.run(argv)
237
238
239 if __name__ == '__main__':
240 main()
241