/usr/share/pyshared/translate/convert/dtd2po.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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2002-2006 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 a Mozilla .dtd UTF-8 localization format to a
Gettext PO localization file.
Uses the po and dtd modules, and the
dtd2po convertor class which is in this module
You can convert back to .dtd using po2dtd.py.
"""
from translate.storage import po
from translate.storage import dtd
from translate.misc import quote
from translate.convert.accesskey import UnitMixer
def is_css_entity(entity):
"""Says if the given entity is likely to contain CSS that should not be
translated."""
if '.' in entity:
prefix, suffix = entity.rsplit('.', 1)
if (suffix in ["height", "width", "unixWidth", "macWidth", "size"] or
suffix.startswith("style")):
return True
return False
class dtd2po:
def __init__(self, blankmsgstr=False, duplicatestyle="msgctxt"):
self.currentgroup = None
self.blankmsgstr = blankmsgstr
self.duplicatestyle = duplicatestyle
self.mixedentities = {}
self.mixer = UnitMixer(dtd.labelsuffixes, dtd.accesskeysuffixes)
def convertcomments(self, dtd_unit, po_unit):
entity = dtd_unit.getid()
if len(entity) > 0:
po_unit.addlocation(entity)
for commenttype, comment in dtd_unit.comments:
# handle groups
if (commenttype == "locgroupstart"):
groupcomment = comment.replace('BEGIN', 'GROUP')
self.currentgroup = groupcomment
elif (commenttype == "locgroupend"):
groupcomment = comment.replace('END', 'GROUP')
self.currentgroup = None
# handle automatic comment
if commenttype == "automaticcomment":
po_unit.addnote(comment, origin="developer")
# handle normal comments
else:
po_unit.addnote(quote.stripcomment(comment), origin="developer")
# handle group stuff
if self.currentgroup is not None:
po_unit.addnote(quote.stripcomment(self.currentgroup),
origin="translator")
if is_css_entity(entity):
po_unit.addnote("Do not translate this. Only change the numeric values if you need this dialogue box to appear bigger",
origin="developer")
def convertstrings(self, dtd_unit, po_unit):
# extract the string, get rid of quoting
unquoted = dtd_unit.source.replace("\r", "")
# escape backslashes... but not if they're for a newline
# unquoted = unquoted.replace("\\", "\\\\").replace("\\\\n", "\\n")
# now split the string into lines and quote them
lines = unquoted.split('\n')
while lines and not lines[0].strip():
del lines[0]
while lines and not lines[-1].strip():
del lines[-1]
# quotes have been escaped already by escapeforpo, so just add the
# start and end quotes
if len(lines) > 1:
po_unit.source = "\n".join([lines[0].rstrip() + ' '] + \
[line.strip() + ' ' for line in lines[1:-1]] + \
[lines[-1].lstrip()])
elif lines:
po_unit.source = lines[0]
else:
po_unit.source = ""
po_unit.target = ""
def convertunit(self, dtd_unit):
"""Converts a simple (non-mixed) dtd unit into a po unit.
Returns None if empty or not for translation.
"""
if dtd_unit is None:
return None
po_unit = po.pounit(encoding="UTF-8")
# remove unwanted stuff
for commentnum in range(len(dtd_unit.comments)):
commenttype, locnote = dtd_unit.comments[commentnum]
# if this is a localization note
if commenttype == 'locnote':
# parse the locnote into the entity and the actual note
typeend = quote.findend(locnote, 'LOCALIZATION NOTE')
# parse the id
idstart = locnote.find('(', typeend)
if idstart == -1:
continue
idend = locnote.find(')', (idstart + 1))
entity = locnote[idstart+1:idend].strip()
# parse the actual note
actualnotestart = locnote.find(':', (idend + 1))
actualnoteend = locnote.find('-->', idend)
actualnote = locnote[actualnotestart+1:actualnoteend].strip()
# if it's for this entity, process it
if dtd_unit.getid() == entity:
# if it says don't translate (and nothing more),
if actualnote.startswith("DONT_TRANSLATE"):
# take out the entity,definition and the
# DONT_TRANSLATE comment
dtd_unit.setid("")
dtd_unit.source = ""
del dtd_unit.comments[commentnum]
# finished this for loop
break
else:
# convert it into an automatic comment, to be
# processed by convertcomments
dtd_unit.comments[commentnum] = ("automaticcomment",
actualnote)
# do a standard translation
self.convertcomments(dtd_unit, po_unit)
self.convertstrings(dtd_unit, po_unit)
if po_unit.isblank() and not po_unit.getlocations():
return None
else:
return po_unit
def convertmixedunit(self, labeldtd, accesskeydtd):
label_unit = self.convertunit(labeldtd)
accesskey_unit = self.convertunit(accesskeydtd)
if label_unit is None:
return accesskey_unit
if accesskey_unit is None:
return label_unit
target_unit = po.pounit(encoding="UTF-8")
return self.mixer.mix_units(label_unit, accesskey_unit, target_unit)
def convertdtdunit(self, store, unit, mixbucket="dtd"):
"""Converts a unit from store to a po unit, keeping track of mixed
entities along the way.
``mixbucket`` can be specified to indicate if the given unit is part of
the template or the translated file.
"""
# keep track of whether accesskey and label were combined
entity = unit.getid()
if entity not in self.mixedentities:
return self.convertunit(unit)
# use special convertmixed unit which produces one pounit with
# both combined for the label and None for the accesskey
alreadymixed = self.mixedentities[entity].get(mixbucket, None)
if alreadymixed:
# we are successfully throwing this away...
return None
elif alreadymixed is False:
# The mix failed before
return self.convertunit(unit)
#assert alreadymixed is None
labelentity, accesskeyentity = self.mixer.find_mixed_pair(self.mixedentities, store, unit)
labeldtd = store.index.get(labelentity, None)
accesskeydtd = store.index.get(accesskeyentity, None)
po_unit = self.convertmixedunit(labeldtd, accesskeydtd)
if po_unit is not None:
if accesskeyentity is not None:
self.mixedentities[accesskeyentity][mixbucket] = True
if labelentity is not None:
self.mixedentities[labelentity][mixbucket] = True
return po_unit
else:
# otherwise the mix failed. add each one separately and
# remember they weren't mixed
if accesskeyentity is not None:
self.mixedentities[accesskeyentity][mixbucket] = False
if labelentity is not None:
self.mixedentities[labelentity][mixbucket] = False
return self.convertunit(unit)
def convertstore(self, dtd_store):
target_store = po.pofile()
targetheader = target_store.init_headers(
x_accelerator_marker="&",
x_merge_on="location",
)
targetheader.addnote("extracted from %s" % dtd_store.filename,
"developer")
dtd_store.makeindex()
self.mixedentities = self.mixer.match_entities(dtd_store.index)
# go through the dtd and convert each unit
for dtd_unit in dtd_store.units:
if not dtd_unit.istranslatable():
continue
po_unit = self.convertdtdunit(dtd_store, dtd_unit)
if po_unit is not None:
target_store.addunit(po_unit)
target_store.removeduplicates(self.duplicatestyle)
return target_store
def mergestore(self, origdtdfile, translateddtdfile):
target_store = po.pofile()
targetheader = target_store.init_headers(
x_accelerator_marker="&",
x_merge_on="location",
)
targetheader.addnote("extracted from %s, %s" % \
(origdtdfile.filename,
translateddtdfile.filename),
"developer")
origdtdfile.makeindex()
#TODO: self.mixedentities is overwritten below, so this is useless:
self.mixedentities = self.mixer.match_entities(origdtdfile.index)
translateddtdfile.makeindex()
self.mixedentities = self.mixer.match_entities(translateddtdfile.index)
# go through the dtd files and convert each unit
for origdtd in origdtdfile.units:
if not origdtd.istranslatable():
continue
origpo = self.convertdtdunit(origdtdfile, origdtd,
mixbucket="orig")
orig_entity = origdtd.getid()
if orig_entity in self.mixedentities:
mixedentitydict = self.mixedentities[orig_entity]
if "orig" not in mixedentitydict:
# this means that the entity is mixed in the translation,
# but not the original - treat as unmixed
mixbucket = "orig"
del self.mixedentities[orig_entity]
elif mixedentitydict["orig"]:
# the original entity is already mixed successfully
mixbucket = "translate"
else:
# ??
mixbucket = "orig"
else:
mixbucket = "translate"
if origpo is None:
# this means its a mixed entity (with accesskey) that's
# already been dealt with)
continue
if orig_entity in translateddtdfile.index:
translateddtd = translateddtdfile.index[orig_entity]
translatedpo = self.convertdtdunit(translateddtdfile,
translateddtd,
mixbucket=mixbucket)
else:
translatedpo = None
if origpo is not None:
if translatedpo is not None and not self.blankmsgstr:
origpo.target = translatedpo.source
target_store.addunit(origpo)
target_store.removeduplicates(self.duplicatestyle)
return target_store
def convertdtd(inputfile, outputfile, templatefile, pot=False,
duplicatestyle="msgctxt"):
"""reads in inputfile and templatefile using dtd, converts using dtd2po,
writes to outputfile"""
android_dtd = False
if hasattr(inputfile, "name"):
# Check if it is an Android DTD file.
if ("embedding/android" in inputfile.name or
"mobile/android/base" in inputfile.name):
android_dtd = True
inputstore = dtd.dtdfile(inputfile, android=android_dtd)
convertor = dtd2po(blankmsgstr=pot, duplicatestyle=duplicatestyle)
if templatefile is None:
outputstore = convertor.convertstore(inputstore)
else:
templatestore = dtd.dtdfile(templatefile, android=android_dtd)
outputstore = convertor.mergestore(templatestore, inputstore)
if outputstore.isempty():
return 0
outputfile.write(str(outputstore))
return 1
def main(argv=None):
from translate.convert import convert
formats = {
"dtd": ("po", convertdtd),
("dtd", "dtd"): ("po", convertdtd),
}
parser = convert.ConvertOptionParser(formats, usetemplates=True,
usepots=True, description=__doc__)
parser.add_duplicates_option()
parser.passthrough.append("pot")
parser.run(argv)
if __name__ == '__main__':
main()
|