1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert an OpenOffice.org (SDF) localization file to XLIFF localization files
24
25 User documentation: http://translate.sourceforge.net/wiki/toolkit/oo2po
26 """
27
28 import sys
29 from urllib import urlencode
30
31 from translate.storage import xliff
32 from translate.storage import oo
33
34
35
36
38
39 - def __init__(self, sourcelanguage, targetlanguage, blankmsgstr=False, long_keys=False):
45
46 - def maketargetunit(self, part1, part2, translators_comment, key, subkey):
47 """makes a base unit (.po or XLIFF) out of a subkey of two parts"""
48
49 text1 = getattr(part1, subkey)
50 if text1 == "":
51 return None
52 text2 = getattr(part2, subkey)
53
54 unit = xliff.xliffunit(text1)
55 unit.target = text2
56 if unit.target:
57 unit.markfuzzy(False)
58 else:
59 unit.markfuzzy(True)
60 unit.addlocation(key + "." + subkey)
61 if getattr(translators_comment, subkey).strip() != "":
62 unit.addnote(getattr(translators_comment, subkey), origin="developer")
63 return unit
64
92
93 - def convertstore(self, theoofile, duplicatestyle="msgctxt"):
94 """converts an entire oo file to a base class format (.po or XLIFF)"""
95 thetargetfile = xliff.xlifffile()
96 thetargetfile.setsourcelanguage(self.sourcelanguage)
97 thetargetfile.settargetlanguage(self.targetlanguage)
98
99 bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
100 urlencode({"subcomponent": "ui",
101 "comment": "",
102 "short_desc": "Localization issue in file: %s" % \
103 theoofile.filename,
104 "component": "l10n",
105 "form_name": "enter_issue",
106 })
107
108 for theoo in theoofile.units:
109 unitlist = self.convertelement(theoo)
110 for unit in unitlist:
111 thetargetfile.addunit(unit)
112 return thetargetfile
113
114
116 """verifies the commandline options"""
117 if not options.targetlanguage:
118 raise ValueError("You must specify the target language.")
119
120
121 -def convertoo(inputfile, outputfile, templates, pot=False, sourcelanguage=None, targetlanguage=None, duplicatestyle="msgctxt", multifilestyle="single"):
122 """reads in stdin using inputstore class, converts using convertorclass, writes to stdout"""
123 inputstore = oo.oofile()
124 if hasattr(inputfile, "filename"):
125 inputfilename = inputfile.filename
126 else:
127 inputfilename = "(input file name not known)"
128 inputstore.filename = inputfilename
129 inputstore.parse(inputfile.read())
130 if not sourcelanguage:
131 testlangtype = targetlanguage or (inputstore and inputstore.languages[0]) or ""
132 if testlangtype.isdigit():
133 sourcelanguage = "01"
134 else:
135 sourcelanguage = "en-US"
136 if not sourcelanguage in inputstore.languages:
137 print >> sys.stderr, "Warning: sourcelanguage '%s' not found in inputfile '%s' (contains %s)" % (sourcelanguage, inputfilename, ", ".join(inputstore.languages))
138 if not pot and targetlanguage and targetlanguage not in inputstore.languages:
139 print >> sys.stderr, "Warning: targetlanguage '%s' not found in inputfile '%s' (contains %s)" % (targetlanguage, inputfilename, ", ".join(inputstore.languages))
140 convertor = oo2xliff(sourcelanguage, targetlanguage, blankmsgstr=pot, long_keys=multifilestyle!="single")
141 outputstore = convertor.convertstore(inputstore, duplicatestyle)
142 if outputstore.isempty():
143 return 0
144 outputfile.write(str(outputstore))
145 return 1
146
147
148 -def main(argv=None):
149 from translate.convert import convert
150 formats = {"oo": ("xlf", convertoo), "sdf": ("xlf", convertoo)}
151
152 archiveformats = {(None, "input"): oo.oomultifile}
153 parser = convert.ArchiveConvertOptionParser(formats, usepots=False, description=__doc__, archiveformats=archiveformats)
154 parser.add_option("-l", "--language", dest="targetlanguage", default=None,
155 help="set target language to extract from oo file (e.g. af-ZA)", metavar="LANG")
156 parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
157 help="set source language code (default en-US)", metavar="LANG")
158 parser.add_option("", "--nonrecursiveinput", dest="allowrecursiveinput", default=True, action="store_false", help="don't treat the input oo as a recursive store")
159 parser.add_duplicates_option()
160 parser.add_multifile_option()
161 parser.passthrough.append("sourcelanguage")
162 parser.passthrough.append("targetlanguage")
163 parser.verifyoptions = verifyoptions
164 parser.run(argv)
165
166
167 if __name__ == '__main__':
168 main()
169