/usr/share/pyshared/translate/convert/oo2xliff.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2003-2008 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
"""Convert an OpenOffice.org (SDF) localization file to XLIFF localization files.
See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/oo2po.html
for examples and usage instructions.
"""
import sys
from urllib import urlencode
from translate.storage import xliff
from translate.storage import oo
# TODO: support using one GSI file as template, another as input (for when English is in one and translation in another)
class oo2xliff:
def __init__(self, sourcelanguage, targetlanguage, blankmsgstr=False, long_keys=False):
"""construct an oo2xliff converter for the specified languages"""
self.sourcelanguage = sourcelanguage
self.targetlanguage = targetlanguage
self.blankmsgstr = blankmsgstr
self.long_keys = long_keys
def maketargetunit(self, part1, part2, translators_comment, key, subkey):
"""makes a base unit (.po or XLIFF) out of a subkey of two parts"""
#TODO: Do better
text1 = getattr(part1, subkey)
if text1 == "":
return None
text2 = getattr(part2, subkey)
unit = xliff.xliffunit(text1)
unit.target = text2
if unit.target:
unit.markfuzzy(False)
else:
unit.markfuzzy(True)
unit.addlocation(key + "." + subkey)
if getattr(translators_comment, subkey).strip() != "":
unit.addnote(getattr(translators_comment, subkey), origin="developer")
return unit
def convertelement(self, theoo):
"""convert an oo element into a list of base units (.po or XLIFF)"""
if self.sourcelanguage in theoo.languages:
part1 = theoo.languages[self.sourcelanguage]
else:
print >> sys.stderr, "/".join(theoo.lines[0].getkey()), "language not found: %s" % (self.sourcelanguage)
return []
if self.blankmsgstr:
# use a blank part2
part2 = oo.ooline()
else:
if self.targetlanguage in theoo.languages:
part2 = theoo.languages[self.targetlanguage]
else:
# if the language doesn't exist, the translation is missing ... so make it blank
part2 = oo.ooline()
if "x-comment" in theoo.languages:
translators_comment = theoo.languages["x-comment"]
else:
translators_comment = oo.ooline()
key = oo.makekey(part1.getkey(), self.long_keys)
unitlist = []
for subkey in ("text", "quickhelptext", "title"):
unit = self.maketargetunit(part1, part2, translators_comment, key, subkey)
if unit is not None:
unitlist.append(unit)
return unitlist
def convertstore(self, theoofile, duplicatestyle="msgctxt"):
"""converts an entire oo file to a base class format (.po or XLIFF)"""
thetargetfile = xliff.xlifffile()
thetargetfile.setsourcelanguage(self.sourcelanguage)
thetargetfile.settargetlanguage(self.targetlanguage)
# create a header for the file
bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
urlencode({"subcomponent": "ui",
"comment": "",
"short_desc": "Localization issue in file: %s" % \
theoofile.filename,
"component": "l10n",
"form_name": "enter_issue",
})
# go through the oo and convert each element
for theoo in theoofile.units:
unitlist = self.convertelement(theoo)
for unit in unitlist:
thetargetfile.addunit(unit)
return thetargetfile
def verifyoptions(options):
"""verifies the commandline options"""
if not options.targetlanguage:
raise ValueError("You must specify the target language.")
def convertoo(inputfile, outputfile, templates, pot=False, sourcelanguage=None, targetlanguage=None, duplicatestyle="msgctxt", multifilestyle="single"):
"""reads in stdin using inputstore class, converts using convertorclass, writes to stdout"""
inputstore = oo.oofile()
if hasattr(inputfile, "filename"):
inputfilename = inputfile.filename
else:
inputfilename = "(input file name not known)"
inputstore.filename = inputfilename
inputstore.parse(inputfile.read())
if not sourcelanguage:
testlangtype = targetlanguage or (inputstore and inputstore.languages[0]) or ""
if testlangtype.isdigit():
sourcelanguage = "01"
else:
sourcelanguage = "en-US"
if not sourcelanguage in inputstore.languages:
print >> sys.stderr, "Warning: sourcelanguage '%s' not found in inputfile '%s' (contains %s)" % (sourcelanguage, inputfilename, ", ".join(inputstore.languages))
if not pot and targetlanguage and targetlanguage not in inputstore.languages:
print >> sys.stderr, "Warning: targetlanguage '%s' not found in inputfile '%s' (contains %s)" % (targetlanguage, inputfilename, ", ".join(inputstore.languages))
convertor = oo2xliff(sourcelanguage, targetlanguage, blankmsgstr=pot, long_keys=(multifilestyle != "single"))
outputstore = convertor.convertstore(inputstore, duplicatestyle)
if outputstore.isempty():
return 0
outputfile.write(str(outputstore))
return 1
def main(argv=None):
from translate.convert import convert
formats = {
"oo": ("xlf", convertoo),
"sdf": ("xlf", convertoo),
}
# always treat the input as an archive unless it is a directory
archiveformats = {(None, "input"): oo.oomultifile}
parser = convert.ArchiveConvertOptionParser(formats, usepots=False,
description=__doc__,
archiveformats=archiveformats)
parser.add_option("-l", "--language", dest="targetlanguage", default=None,
help="set target language to extract from oo file (e.g. af-ZA)",
metavar="LANG")
parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
help="set source language code (default en-US)", metavar="LANG")
parser.add_option("", "--nonrecursiveinput", dest="allowrecursiveinput",
default=True, action="store_false",
help="don't treat the input oo as a recursive store")
parser.add_duplicates_option()
parser.add_multifile_option()
parser.passthrough.append("sourcelanguage")
parser.passthrough.append("targetlanguage")
parser.verifyoptions = verifyoptions
parser.run(argv)
if __name__ == '__main__':
main()
|