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