| 1 |
|
| 2 |
|
| 3 |
. |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
VERSION = "1.0.5" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
import sys, traceback |
| 35 |
import libxml2 |
| 36 |
import gettext |
| 37 |
import os |
| 38 |
import re |
| 39 |
|
| 40 |
class NoneTranslations: |
| 41 |
def gettext(self, message): |
| 42 |
return None |
| 43 |
|
| 44 |
def lgettext(self, message): |
| 45 |
return None |
| 46 |
|
| 47 |
def ngettext(self, msgid1, msgid2, n): |
| 48 |
return None |
| 49 |
|
| 50 |
def lngettext(self, msgid1, msgid2, n): |
| 51 |
return None |
| 52 |
|
| 53 |
def ugettext(self, message): |
| 54 |
return None |
| 55 |
|
| 56 |
def ungettext(self, msgid1, msgid2, n): |
| 57 |
return None |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
class MessageOutput: |
| 62 |
def __init__(self, with_translations = 0): |
| 63 |
self.messages = [] |
| 64 |
self.comments = {} |
| 65 |
self.linenos = {} |
| 66 |
self.nowrap = {} |
| 67 |
if with_translations: |
| 68 |
self.translations = [] |
| 69 |
self.do_translations = with_translations |
| 70 |
self.output_msgstr = 0 |
| 71 |
|
| 72 |
def translationsFollow(self): |
| 73 |
"""Indicate that what follows are translations.""" |
| 74 |
self.output_msgstr = 1 |
| 75 |
|
| 76 |
def setFilename(self, filename): |
| 77 |
self.filename = filename |
| 78 |
|
| 79 |
def outputMessage(self, text, lineno = 0, comment = None, spacepreserve = 0, tag = None): |
| 80 |
"""Adds a string to the list of messages.""" |
| 81 |
if (text.strip() != ''): |
| 82 |
t = escapePoString(normalizeString(text, not spacepreserve)) |
| 83 |
if self.output_msgstr: |
| 84 |
self.translations.append(t) |
| 85 |
return |
| 86 |
|
| 87 |
if self.do_translations or (not t in self.messages): |
| 88 |
self.messages.append(t) |
| 89 |
if spacepreserve: |
| 90 |
self.nowrap[t] = 1 |
| 91 |
if t in self.linenos.keys(): |
| 92 |
self.linenos[t].append((self.filename, tag, lineno)) |
| 93 |
else: |
| 94 |
self.linenos[t] = [ (self.filename, tag, lineno) ] |
| 95 |
if (not self.do_translations) and comment and not t in self.comments: |
| 96 |
self.comments[t] = comment |
| 97 |
else: |
| 98 |
if t in self.linenos.keys(): |
| 99 |
self.linenos[t].append((self.filename, tag, lineno)) |
| 100 |
else: |
| 101 |
self.linenos[t] = [ (self.filename, tag, lineno) ] |
| 102 |
if comment and not t in self.comments: |
| 103 |
self.comments[t] = comment |
| 104 |
|
| 105 |
def outputHeader(self, out): |
| 106 |
from time import gmtime, strftime |
| 107 |
out.write("""msgid "" |
| 108 |
msgstr "" |
| 109 |
"Project-Id-Version: PACKAGE VERSION\\n" |
| 110 |
"POT-Creation-Date: %s\\n" |
| 111 |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" |
| 112 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" |
| 113 |
"Language-Team: LANGUAGE <[email protected]>\\n" |
| 114 |
"MIME-Version: 1.0\\n" |
| 115 |
"Content-Type: text/plain; charset=UTF-8\\n" |
| 116 |
"Content-Transfer-Encoding: 8bit\\n" |
| 117 |
|
| 118 |
""" % (strftime("%Y-%m-%d %H:%M +0000", gmtime()))) |
| 119 |
|
| 120 |
def outputAll(self, out): |
| 121 |
self.outputHeader(out) |
| 122 |
|
| 123 |
for k in self.messages: |
| 124 |
references = "" |
| 125 |
tagstr = "" |
| 126 |
tags = [] |
| 127 |
nt = 0 |
| 128 |
nr = 0 |
| 129 |
verbatim = 0 |
| 130 |
for reference in self.linenos[k]: |
| 131 |
if nr < 4: |
| 132 |
references += "%s:%d " % (reference[0], reference[2]) |
| 133 |
nr += 1 |
| 134 |
if(reference[1] in ["programlisting", "screen", "synopsis", "funcsynopsis", "command"]): |
| 135 |
verbatim += 1 |
| 136 |
if(reference[1] not in tags): |
| 137 |
tags.append(reference[1]) |
| 138 |
if nt < 4: |
| 139 |
tagstr += "(" + str(reference[1]) + "), " |
| 140 |
nt += 1 |
| 141 |
if verbatim > 0: continue |
| 142 |
if k in self.comments: |
| 143 |
out.write("#. %s\n" % (self.comments[k].replace("\n","\n#. "))) |
| 144 |
out.write("#.%s\n" % (tagstr[0 : len(tagstr) - 2])) |
| 145 |
out.write("#: %s\n" % (references[0 : len(references) - 1])) |
| 146 |
if k in self.nowrap and self.nowrap[k]: |
| 147 |
out.write("#, no-wrap\n") |
| 148 |
out.write("msgid \"%s\"\n" % (k)) |
| 149 |
translation = "" |
| 150 |
if self.do_translations: |
| 151 |
if len(self.translations)>0: |
| 152 |
translation = self.translations.pop(0) |
| 153 |
if translation == k: |
| 154 |
translation = "" |
| 155 |
out.write("msgstr \"%s\"\n\n" % (translation)) |
| 156 |
|
| 157 |
|
| 158 |
def normalizeNode(node): |
| 159 |
|
| 160 |
if not node: |
| 161 |
return |
| 162 |
elif isSpacePreserveNode(node): |
| 163 |
return |
| 164 |
elif node.isText(): |
| 165 |
if node.isBlankNode(): |
| 166 |
if expand_entities or ( not (node.prev and not node.prev.isBlankNode() |
| 167 |
and node.next and not node.next.isBlankNode()) ): |
| 168 |
|
| 169 |
node.setContent('') |
| 170 |
else: |
| 171 |
node.setContent(re.sub('\s+',' ', node.content)) |
| 172 |
|
| 173 |
elif node.children and node.type == 'element': |
| 174 |
child = node.children |
| 175 |
while child: |
| 176 |
normalizeNode(child) |
| 177 |
child = child.next |
| 178 |
|
| 179 |
def normalizeString(text, ignorewhitespace = 1): |
| 180 |
"""Normalizes string to be used as key for gettext lookup. |
| 181 |
|
| 182 |
Removes all unnecessary whitespace.""" |
| 183 |
if not ignorewhitespace: |
| 184 |
return text |
| 185 |
try: |
| 186 |
|
| 187 |
dtd = doc.intSubset() |
| 188 |
tmp = dtd.serialize('utf-8') |
| 189 |
tmp = tmp + '<norm>%s</norm>' % text |
| 190 |
except: |
| 191 |
tmp = '<norm>%s</norm>' % text |
| 192 |
|
| 193 |
try: |
| 194 |
ctxt = libxml2.createDocParserCtxt(tmp) |
| 195 |
if expand_entities: |
| 196 |
ctxt.replaceEntities(1) |
| 197 |
ctxt.parseDocument() |
| 198 |
tree = ctxt.doc() |
| 199 |
newnode = tree.getRootElement() |
| 200 |
except: |
| 201 |
print >> sys.stderr, """Error while normalizing string as XML:\n"%s"\n""" % (text) |
| 202 |
return text |
| 203 |
|
| 204 |
normalizeNode(newnode) |
| 205 |
|
| 206 |
result = '' |
| 207 |
child = newnode.children |
| 208 |
while child: |
| 209 |
result += child.serialize('utf-8') |
| 210 |
child = child.next |
| 211 |
|
| 212 |
result = re.sub('^ ','', result) |
| 213 |
result = re.sub(' $','', result) |
| 214 |
|
| 215 |
return result |
| 216 |
|
| 217 |
def stringForEntity(node): |
| 218 |
"""Replaces entities in the node.""" |
| 219 |
text = node.serialize('utf-8') |
| 220 |
try: |
| 221 |
|
| 222 |
dtd = node.doc.intSubset() |
| 223 |
tmp = dtd.serialize('utf-8') + '<norm>%s</norm>' % text |
| 224 |
next = 1 |
| 225 |
except: |
| 226 |
tmp = '<norm>%s</norm>' % text |
| 227 |
next = 0 |
| 228 |
|
| 229 |
ctxt = libxml2.createDocParserCtxt(tmp) |
| 230 |
if expand_entities: |
| 231 |
ctxt.replaceEntities(1) |
| 232 |
ctxt.parseDocument() |
| 233 |
tree = ctxt.doc() |
| 234 |
if next: |
| 235 |
newnode = tree.children.next |
| 236 |
else: |
| 237 |
newnode = tree.children |
| 238 |
|
| 239 |
result = '' |
| 240 |
child = newnode.children |
| 241 |
while child: |
| 242 |
result += child.serialize('utf-8') |
| 243 |
child = child.next |
| 244 |
|
| 245 |
return result |
| 246 |
|
| 247 |
|
| 248 |
def escapePoString(text): |
| 249 |
return text.replace('\\','\\\\').replace('"', "\\\"").replace("\n","\\n").replace("\t","\\t") |
| 250 |
|
| 251 |
def unEscapePoString(text): |
| 252 |
return text.replace('\\"', '"').replace('\\\\','\\') |
| 253 |
|
| 254 |
def getTranslation(text, spacepreserve = 0): |
| 255 |
"""Returns a translation via gettext for specified snippet. |
| 256 |
|
| 257 |
text should be a string to look for, spacepreserve set to 1 |
| 258 |
when spaces should be preserved. |
| 259 |
""" |
| 260 |
|
| 261 |
text = normalizeString(text, not spacepreserve) |
| 262 |
if (text.strip() == ''): |
| 263 |
return text |
| 264 |
global gt |
| 265 |
if gt: |
| 266 |
res = gt.ugettext(text.decode('utf-8')) |
| 267 |
return res |
| 268 |
|
| 269 |
return text |
| 270 |
|
| 271 |
def myAttributeSerialize(node): |
| 272 |
result = '' |
| 273 |
if node.children: |
| 274 |
child = node.children |
| 275 |
while child: |
| 276 |
if child.type=='text': |
| 277 |
result += doc.encodeEntitiesReentrant(child.content) |
| 278 |
elif child.type=='entity_ref': |
| 279 |
if not expand_entities: |
| 280 |
result += '&' + child.name + ';' |
| 281 |
else: |
| 282 |
result += child.content.decode('utf-8') |
| 283 |
else: |
| 284 |
result += myAttributeSerialize(child) |
| 285 |
child = child.next |
| 286 |
else: |
| 287 |
result = node.serialize('utf-8') |
| 288 |
return result |
| 289 |
|
| 290 |
def startTagForNode(node): |
| 291 |
if not node: |
| 292 |
return 0 |
| 293 |
|
| 294 |
result = node.name |
| 295 |
params = '' |
| 296 |
if node.properties: |
| 297 |
for p in node.properties: |
| 298 |
if p.type == 'attribute': |
| 299 |
try: |
| 300 |
nsprop = p.ns().name + ":" + p.name |
| 301 |
except: |
| 302 |
nsprop = p.name |
| 303 |
params += " %s=\"%s\"" % (nsprop, myAttributeSerialize(p)) |
| 304 |
return result+params |
| 305 |
|
| 306 |
def endTagForNode(node): |
| 307 |
if not node: |
| 308 |
return 0 |
| 309 |
|
| 310 |
result = node.name |
| 311 |
return result |
| 312 |
|
| 313 |
def isFinalNode(node): |
| 314 |
if automatic: |
| 315 |
auto = autoNodeIsFinal(node) |
| 316 |
|
| 317 |
|
| 318 |
parent = node.parent |
| 319 |
while parent and auto: |
| 320 |
auto = not autoNodeIsFinal(parent) |
| 321 |
parent = parent.parent |
| 322 |
return auto |
| 323 |
|
| 324 |
if node.type == 'element' and node.name in ultimate_tags: |
| 325 |
return 1 |
| 326 |
elif node.children: |
| 327 |
final_children = 1 |
| 328 |
child = node.children |
| 329 |
while child and final_children: |
| 330 |
if not child.isBlankNode() and child.type != 'comment' and not isFinalNode(child): |
| 331 |
final_children = 0 |
| 332 |
child = child.next |
| 333 |
if final_children: |
| 334 |
return 1 |
| 335 |
return 0 |
| 336 |
|
| 337 |
def ignoreNode(node): |
| 338 |
if automatic: |
| 339 |
if node.type in ('dtd', 'comment'): |
| 340 |
return 1 |
| 341 |
else: |
| 342 |
return 0 |
| 343 |
else: |
| 344 |
if isFinalNode(node): |
| 345 |
return 0 |
| 346 |
if node.name in ignored_tags or node.type in ('dtd', 'comment'): |
| 347 |
return 1 |
| 348 |
return 0 |
| 349 |
|
| 350 |
def isSpacePreserveNode(node): |
| 351 |
pres = node.getSpacePreserve() |
| 352 |
if pres == 1: |
| 353 |
return 1 |
| 354 |
else: |
| 355 |
if CurrentXmlMode and (node.name in CurrentXmlMode.getSpacePreserveTags()): |
| 356 |
return 1 |
| 357 |
else: |
| 358 |
return 0 |
| 359 |
|
| 360 |
def getCommentForNode(node): |
| 361 |
"""Walk through previous siblings until a comment is found, or other element. |
| 362 |
|
| 363 |
Only whitespace is allowed between comment and current node.""" |
| 364 |
prev = node.prev |
| 365 |
while prev and prev.type == 'text' and prev.content.strip() == '': |
| 366 |
prev = prev.prev |
| 367 |
if prev and prev.type == 'comment': |
| 368 |
return prev.content.strip() |
| 369 |
else: |
| 370 |
return None |
| 371 |
|
| 372 |
def replaceAttributeContentsWithText(node,text): |
| 373 |
node.setContent(text) |
| 374 |
|
| 375 |
def replaceNodeContentsWithText(node,text): |
| 376 |
"""Replaces all subnodes of a node with contents of text treated as XML.""" |
| 377 |
|
| 378 |
if node.children: |
| 379 |
starttag = startTagForNode(node) |
| 380 |
endtag = endTagForNode(node) |
| 381 |
|
| 382 |
|
| 383 |
tmp = '<?xml version="1.0" encoding="utf-8" ?>' |
| 384 |
try: |
| 385 |
dtd = doc.intSubset() |
| 386 |
tmp = tmp + dtd.serialize('utf-8') |
| 387 |
except libxml2.treeError: |
| 388 |
pass |
| 389 |
|
| 390 |
content = '<%s>%s</%s>' % (starttag, text, endtag) |
| 391 |
tmp = tmp + content.encode('utf-8') |
| 392 |
|
| 393 |
newnode = None |
| 394 |
try: |
| 395 |
ctxt = libxml2.createDocParserCtxt(tmp) |
| 396 |
ctxt.replaceEntities(0) |
| 397 |
ctxt.parseDocument() |
| 398 |
newnode = ctxt.doc() |
| 399 |
except: |
| 400 |
pass |
| 401 |
|
| 402 |
if not newnode: |
| 403 |
print >> sys.stderr, """Error while parsing translation as XML:\n"%s"\n""" % (text.encode('utf-8')) |
| 404 |
return |
| 405 |
|
| 406 |
newelem = newnode.getRootElement() |
| 407 |
|
| 408 |
if newelem and newelem.children: |
| 409 |
free = node.children |
| 410 |
while free: |
| 411 |
next = free.next |
| 412 |
free.unlinkNode() |
| 413 |
free = next |
| 414 |
|
| 415 |
if node: |
| 416 |
copy = newelem.copyNodeList() |
| 417 |
next = node.next |
| 418 |
node.replaceNode(newelem.copyNodeList()) |
| 419 |
node.next = next |
| 420 |
|
| 421 |
else: |
| 422 |
|
| 423 |
pass |
| 424 |
else: |
| 425 |
node.setContent(text) |
| 426 |
|
| 427 |
def autoNodeIsFinal(node): |
| 428 |
"""Returns 1 if node is text node, contains non-whitespace text nodes or entities.""" |
| 429 |
if hasattr(node, '__autofinal__'): |
| 430 |
return node.__autofinal__ |
| 431 |
if node.name in ignored_tags: |
| 432 |
node.__autofinal__ = 0 |
| 433 |
return 0 |
| 434 |
if node.isText() and node.content.strip()!='': |
| 435 |
node.__autofinal__ = 1 |
| 436 |
return 1 |
| 437 |
final = 0 |
| 438 |
child = node.children |
| 439 |
while child: |
| 440 |
if child.type in ['text'] and child.content.strip()!='': |
| 441 |
final = 1 |
| 442 |
break |
| 443 |
child = child.next |
| 444 |
|
| 445 |
node.__autofinal__ = final |
| 446 |
return final |
| 447 |
|
| 448 |
|
| 449 |
def worthOutputting(node, noauto = 0): |
| 450 |
"""Returns 1 if node is "worth outputting", otherwise 0. |
| 451 |
|
| 452 |
Node is "worth outputting", if none of the parents |
| 453 |
isFinalNode, and it contains non-blank text and entities. |
| 454 |
""" |
| 455 |
if noauto and hasattr(node, '__worth__'): |
| 456 |
return node.__worth__ |
| 457 |
elif not noauto and hasattr(node, '__autoworth__'): |
| 458 |
return node.__autoworth__ |
| 459 |
worth = 1 |
| 460 |
parent = node.parent |
| 461 |
final = isFinalNode(node) and node.name not in ignored_tags |
| 462 |
while not final and parent: |
| 463 |
if isFinalNode(parent): |
| 464 |
final = 1 |
| 465 |
if final and (parent.name not in ignored_tags) and worthOutputting(parent): |
| 466 |
worth = 0 |
| 467 |
break |
| 468 |
parent = parent.parent |
| 469 |
if not worth: |
| 470 |
node.__worth__ = 0 |
| 471 |
return 0 |
| 472 |
|
| 473 |
if noauto: |
| 474 |
node.__worth__ = worth |
| 475 |
return worth |
| 476 |
else: |
| 477 |
node.__autoworth__ = autoNodeIsFinal(node) |
| 478 |
return node.__autoworth__ |
| 479 |
|
| 480 |
def processAttribute(node, attr): |
| 481 |
if not node or not attr or not worthOutputting(node=node, noauto=1): |
| 482 |
return |
| 483 |
|
| 484 |
outtxt = attr.content |
| 485 |
if mode=='merge': |
| 486 |
translation = getTranslation(outtxt, 0) |
| 487 |
replaceAttributeContentsWithText(attr, translation.encode('utf-8')) |
| 488 |
else: |
| 489 |
msg.outputMessage(outtxt, node.lineNo(), "", 0, |
| 490 |
node.name + ":" + attr.name) |
| 491 |
|
| 492 |
def processElementTag(node, replacements, restart = 0): |
| 493 |
"""Process node with node.type == 'element'.""" |
| 494 |
if node.type == 'element': |
| 495 |
|
| 496 |
if node.properties and len(treated_attributes): |
| 497 |
for p in node.properties: |
| 498 |
if p.name in treated_attributes: |
| 499 |
processAttribute(node, p) |
| 500 |
|
| 501 |
outtxt = '' |
| 502 |
if restart: |
| 503 |
myrepl = [] |
| 504 |
else: |
| 505 |
myrepl = replacements |
| 506 |
|
| 507 |
submsgs = [] |
| 508 |
|
| 509 |
child = node.children |
| 510 |
while child: |
| 511 |
if (isFinalNode(child)) or (child.type == 'element' and worthOutputting(child)): |
| 512 |
myrepl.append(processElementTag(child, myrepl, 1)) |
| 513 |
outtxt += '<placeholder-%d/>' % (len(myrepl)) |
| 514 |
else: |
| 515 |
if child.type == 'element': |
| 516 |
(starttag, content, endtag, translation) = processElementTag(child, myrepl, 0) |
| 517 |
outtxt += '<%s>%s</%s>' % (starttag, content, endtag) |
| 518 |
else: |
| 519 |
outtxt += doSerialize(child) |
| 520 |
|
| 521 |
child = child.next |
| 522 |
|
| 523 |
if mode == 'merge': |
| 524 |
translation = getTranslation(outtxt, isSpacePreserveNode(node)) |
| 525 |
else: |
| 526 |
translation = outtxt.decode('utf-8') |
| 527 |
|
| 528 |
starttag = startTagForNode(node) |
| 529 |
endtag = endTagForNode(node) |
| 530 |
|
| 531 |
worth = worthOutputting(node) |
| 532 |
if not translation: |
| 533 |
translation = outtxt.decode('utf-8') |
| 534 |
if worth and mark_untranslated: node.setLang('C') |
| 535 |
|
| 536 |
if restart or worth: |
| 537 |
i = 0 |
| 538 |
while i < len(myrepl): |
| 539 |
replacement = '<%s>%s</%s>' % (myrepl[i][0], myrepl[i][3], myrepl[i][2]) |
| 540 |
i += 1 |
| 541 |
translation = translation.replace('<placeholder-%d/>' % (i), replacement) |
| 542 |
|
| 543 |
if worth: |
| 544 |
if mode == 'merge': |
| 545 |
replaceNodeContentsWithText(node, translation) |
| 546 |
else: |
| 547 |
msg.outputMessage(outtxt, node.lineNo(), getCommentForNode(node), isSpacePreserveNode(node), tag = node.name) |
| 548 |
|
| 549 |
return (starttag, outtxt, endtag, translation) |
| 550 |
else: |
| 551 |
raise Exception("You must pass node with node.type=='element'.") |
| 552 |
|
| 553 |
|
| 554 |
def isExternalGeneralParsedEntity(node): |
| 555 |
if (node and node.type=='entity_ref'): |
| 556 |
try: |
| 557 |
|
| 558 |
tmp = file(".xml2po-entitychecking","w+") |
| 559 |
node.debugDumpNode(tmp,0) |
| 560 |
tmp.seek(0) |
| 561 |
tmpstr = tmp.read() |
| 562 |
tmp.close() |
| 563 |
os.remove(".xml2po-entitychecking") |
| 564 |
except: |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
return 0 |
| 570 |
if tmpstr.find('EXTERNAL_GENERAL_PARSED_ENTITY') != -1: |
| 571 |
return 1 |
| 572 |
else: |
| 573 |
return 0 |
| 574 |
else: |
| 575 |
return 0 |
| 576 |
|
| 577 |
def doSerialize(node): |
| 578 |
"""Serializes a node and its children, emitting PO messages along the way. |
| 579 |
|
| 580 |
node is the node to serialize, first indicates whether surrounding |
| 581 |
tags should be emitted as well. |
| 582 |
""" |
| 583 |
|
| 584 |
if ignoreNode(node): |
| 585 |
return '' |
| 586 |
elif not node.children: |
| 587 |
return node.serialize("utf-8") |
| 588 |
elif node.type == 'entity_ref': |
| 589 |
if isExternalGeneralParsedEntity(node): |
| 590 |
return node.serialize('utf-8') |
| 591 |
else: |
| 592 |
return stringForEntity(node) |
| 593 |
elif node.type == 'entity_decl': |
| 594 |
return node.serialize('utf-8') |
| 595 |
elif node.type == 'text': |
| 596 |
return node.serialize('utf-8') |
| 597 |
elif node.type == 'element': |
| 598 |
repl = [] |
| 599 |
(starttag, content, endtag, translation) = processElementTag(node, repl, 1) |
| 600 |
return '<%s>%s</%s>' % (starttag, content, endtag) |
| 601 |
else: |
| 602 |
child = node.children |
| 603 |
outtxt = '' |
| 604 |
while child: |
| 605 |
outtxt += doSerialize(child) |
| 606 |
child = child.next |
| 607 |
return outtxt |
| 608 |
|
| 609 |
|
| 610 |
def read_finaltags(filelist): |
| 611 |
if CurrentXmlMode: |
| 612 |
return CurrentXmlMode.getFinalTags() |
| 613 |
else: |
| 614 |
defaults = ['para', 'title', 'releaseinfo', 'revnumber', |
| 615 |
'date', 'itemizedlist', 'orderedlist', |
| 616 |
'variablelist', 'varlistentry', 'term' ] |
| 617 |
return defaults |
| 618 |
|
| 619 |
def read_ignoredtags(filelist): |
| 620 |
if CurrentXmlMode: |
| 621 |
return CurrentXmlMode.getIgnoredTags() |
| 622 |
else: |
| 623 |
defaults = ['itemizedlist', 'orderedlist', 'variablelist', |
| 624 |
'varlistentry' ] |
| 625 |
return defaults |
| 626 |
|
| 627 |
def read_treatedattributes(filelist): |
| 628 |
if CurrentXmlMode: |
| 629 |
return CurrentXmlMode.getTreatedAttributes() |
| 630 |
else: |
| 631 |
return [] |
| 632 |
|
| 633 |
|
| 634 |
def tryToUpdate(allargs, lang): |
| 635 |
|
| 636 |
print >>sys.stderr, "OVDI!" |
| 637 |
command = allargs[0] |
| 638 |
args = allargs[1:] |
| 639 |
opts, args = getopt.getopt(args, 'avhm:ket:o:p:u:', |
| 640 |
['automatic-tags','version', 'help', 'keep-entities', 'extract-all-entities', 'merge', 'translation=', |
| 641 |
'output=', 'po-file=', 'update-translation=' ]) |
| 642 |
for opt, arg in opts: |
| 643 |
if opt in ('-a', '--automatic-tags'): |
| 644 |
command += " -a" |
| 645 |
elif opt in ('-k', '--keep-entities'): |
| 646 |
command += " -k" |
| 647 |
elif opt in ('-e', '--extract-all-entities'): |
| 648 |
command += " -e" |
| 649 |
elif opt in ('-m', '--mode'): |
| 650 |
command += " -m %s" % arg |
| 651 |
elif opt in ('-o', '--output'): |
| 652 |
sys.stderr.write("Error: Option '-o' is not yet supported when updating translations directly.\n") |
| 653 |
sys.exit(8) |
| 654 |
elif opt in ('-v', '--version'): |
| 655 |
print VERSION |
| 656 |
sys.exit(0) |
| 657 |
elif opt in ('-h', '--help'): |
| 658 |
sys.stderr.write("Error: If you want help, please use `%s --help' without '-u' option.\n" % (allargs[0])) |
| 659 |
sys.exit(9) |
| 660 |
elif opt in ('-u', '--update-translation'): |
| 661 |
pass |
| 662 |
else: |
| 663 |
sys.stderr.write("Error: Option `%s' is not supported with option `-u'.\n" % (opt)) |
| 664 |
sys.exit(9) |
| 665 |
|
| 666 |
while args: |
| 667 |
command += " " + args.pop() |
| 668 |
|
| 669 |
file = lang |
| 670 |
|
| 671 |
sys.stderr.write("Merging translations for %s: " % (lang)) |
| 672 |
result = os.system("%s | msgmerge -o .tmp.%s.po %s -" % (command, lang, file)) |
| 673 |
if result: |
| 674 |
sys.exit(10) |
| 675 |
else: |
| 676 |
result = os.system("mv .tmp.%s.po %s" % (lang, file)) |
| 677 |
if result: |
| 678 |
sys.stderr.write("Error: cannot rename file.\n") |
| 679 |
sys.exit(11) |
| 680 |
else: |
| 681 |
os.system("msgfmt -cv -o %s %s" % (NULL_STRING, file)) |
| 682 |
sys.exit(0) |
| 683 |
|
| 684 |
def load_mode(modename): |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
try: |
| 689 |
sys.path.append(submodes_path) |
| 690 |
module = __import__(modename) |
| 691 |
modeModule = '%sXmlMode' % modename |
| 692 |
return getattr(module, modeModule) |
| 693 |
except: |
| 694 |
traceback.print_exc(file=sys.stderr) |
| 695 |
return None |
| 696 |
|
| 697 |
def xml_error_handler(arg, ctxt): |
| 698 |
pass |
| 699 |
|
| 700 |
libxml2.registerErrorHandler(xml_error_handler, None) |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
if __name__ != '__main__': raise NotImplementedError |
| 705 |
|
| 706 |
|
| 707 |
submodes_path = os.path.dirname(os.path.realpath(sys.argv[0])) + "/xml2po-modes" |
| 708 |
default_mode = 'docbook' |
| 709 |
|
| 710 |
filename = '' |
| 711 |
origxml = '' |
| 712 |
mofile = '' |
| 713 |
gt = None |
| 714 |
ultimate = [ ] |
| 715 |
ignored = [ ] |
| 716 |
filenames = [ ] |
| 717 |
translationlanguage = '' |
| 718 |
|
| 719 |
mode = 'pot' |
| 720 |
automatic = 0 |
| 721 |
expand_entities = 1 |
| 722 |
mark_untranslated = 0 |
| 723 |
expand_all_entities = 0 |
| 724 |
|
| 725 |
output = '-' |
| 726 |
|
| 727 |
NULL_STRING = '/dev/null' |
| 728 |
if not os.path.exists('/dev/null'): NULL_STRING = 'NUL' |
| 729 |
|
| 730 |
import getopt, fileinput |
| 731 |
|
| 732 |
def usage (with_help = False): |
| 733 |
print >> sys.stderr, "Usage: %s [OPTIONS] [XMLFILE]..." % (sys.argv[0]) |
| 734 |
if (with_help): |
| 735 |
print >> sys.stderr, """ |
| 736 |
OPTIONS may be some of: |
| 737 |
-a --automatic-tags Automatically decides if tags are to be considered |
| 738 |
"final" or not |
| 739 |
-k --keep-entities Don't expand entities |
| 740 |
-e --expand-all-entities Expand ALL entities (including SYSTEM ones) |
| 741 |
-m --mode=TYPE Treat tags as type TYPE (default: docbook) |
| 742 |
-o --output=FILE Print resulting text (XML or POT) to FILE |
| 743 |
-p --po-file=FILE Specify PO file containing translation, and merge |
| 744 |
Overwrites temporary file .xml2po.mo. |
| 745 |
-r --reuse=FILE Specify translated XML file with the same structure |
| 746 |
-t --translation=FILE Specify MO file containing translation, and merge |
| 747 |
-u --update-translation=LANG.po Updates a PO file using msgmerge program |
| 748 |
|
| 749 |
-l --language=LANG Set language of the translation to LANG |
| 750 |
--mark-untranslated Set 'xml:lang="C"' on untranslated tags |
| 751 |
|
| 752 |
-v --version Output version of the xml2po program |
| 753 |
|
| 754 |
-h --help Output this message |
| 755 |
|
| 756 |
EXAMPLES: |
| 757 |
To create a POTemplate book.pot from input files chapter1.xml and |
| 758 |
chapter2.xml, run the following: |
| 759 |
%s -o book.pot chapter1.xml chapter2.xml |
| 760 |
|
| 761 |
After translating book.pot into de.po, merge the translations back, |
| 762 |
using -p option for each XML file: |
| 763 |
%s -p de.po chapter1.xml > chapter1.de.xml |
| 764 |
%s -p de.po chapter2.xml > chapter2.de.xml |
| 765 |
""" % (sys.argv[0], sys.argv[0], sys.argv[0]) |
| 766 |
sys.exit(0) |
| 767 |
|
| 768 |
if len(sys.argv) < 2: usage() |
| 769 |
|
| 770 |
args = sys.argv[1:] |
| 771 |
try: opts, args = getopt.getopt(args, 'avhkem:t:o:p:u:r:l:', |
| 772 |
['automatic-tags','version', 'help', 'keep-entities', 'expand-all-entities', 'mode=', 'translation=', |
| 773 |
'output=', 'po-file=', 'update-translation=', 'reuse=', 'language=', 'mark-untranslated' ]) |
| 774 |
except getopt.GetoptError: usage(True) |
| 775 |
|
| 776 |
for opt, arg in opts: |
| 777 |
if opt in ('-m', '--mode'): |
| 778 |
default_mode = arg |
| 779 |
if opt in ('-a', '--automatic-tags'): |
| 780 |
automatic = 1 |
| 781 |
elif opt in ('-k', '--keep-entities'): |
| 782 |
expand_entities = 0 |
| 783 |
elif opt in ('--mark-untranslated',): |
| 784 |
mark_untranslated = 1 |
| 785 |
elif opt in ('-e', '--expand-all-entities'): |
| 786 |
expand_all_entities = 1 |
| 787 |
elif opt in ('-l', '--language'): |
| 788 |
translationlanguage = arg |
| 789 |
elif opt in ('-t', '--translation'): |
| 790 |
mofile = arg |
| 791 |
mode = 'merge' |
| 792 |
if translationlanguage == '': translationlanguage = os.path.split(os.path.splitext(mofile)[0])[1] |
| 793 |
elif opt in ('-r', '--reuse'): |
| 794 |
origxml = arg |
| 795 |
elif opt in ('-u', '--update-translation'): |
| 796 |
tryToUpdate(sys.argv, arg) |
| 797 |
elif opt in ('-p', '--po-file'): |
| 798 |
mofile = ".xml2po.mo" |
| 799 |
pofile = arg |
| 800 |
if translationlanguage == '': translationlanguage = os.path.split(os.path.splitext(pofile)[0])[1] |
| 801 |
os.system("msgfmt -o %s %s >%s" % (mofile, pofile, NULL_STRING)) and sys.exit(7) |
| 802 |
mode = 'merge' |
| 803 |
elif opt in ('-o', '--output'): |
| 804 |
output = arg |
| 805 |
elif opt in ('-v', '--version'): |
| 806 |
print VERSION |
| 807 |
sys.exit(0) |
| 808 |
elif opt in ('-h', '--help'): |
| 809 |
usage(True) |
| 810 |
|
| 811 |
|
| 812 |
while args: |
| 813 |
filenames.append(args.pop()) |
| 814 |
|
| 815 |
if len(filenames) > 1 and mode=='merge': |
| 816 |
print >> sys.stderr, "Error: You can merge translations with only one XML file at a time." |
| 817 |
sys.exit(2) |
| 818 |
|
| 819 |
try: |
| 820 |
CurrentXmlMode = load_mode(default_mode)() |
| 821 |
except: |
| 822 |
CurrentXmlMode = None |
| 823 |
print >> sys.stderr, "Warning: cannot load module '%s', using automatic detection (-a)." % (default_mode) |
| 824 |
automatic = 1 |
| 825 |
|
| 826 |
if mode=='merge' and mofile=='': |
| 827 |
print >> sys.stderr, "Error: You must specify MO file when merging translations." |
| 828 |
sys.exit(3) |
| 829 |
|
| 830 |
if mofile: |
| 831 |
try: |
| 832 |
mfile = open(mofile, "rb") |
| 833 |
|
| 834 |
gt = gettext.GNUTranslations(mfile) |
| 835 |
gt.add_fallback(NoneTranslations()) |
| 836 |
except: |
| 837 |
print >> sys.stderr, "Can't open MO file '%s'." % (mofile) |
| 838 |
|
| 839 |
ultimate_tags = read_finaltags(ultimate) |
| 840 |
ignored_tags = read_ignoredtags(ignored) |
| 841 |
treated_attributes = read_treatedattributes(ignored) |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
semitrans = {} |
| 846 |
PlaceHolder = 0 |
| 847 |
if origxml == '': |
| 848 |
msg = MessageOutput() |
| 849 |
else: |
| 850 |
filenames.append(origxml) |
| 851 |
msg = MessageOutput(1) |
| 852 |
|
| 853 |
for filename in filenames: |
| 854 |
try: |
| 855 |
if filename == origxml: |
| 856 |
msg.translationsFollow() |
| 857 |
ctxt = libxml2.createFileParserCtxt(filename) |
| 858 |
ctxt.lineNumbers(1) |
| 859 |
if expand_all_entities: |
| 860 |
ctxt.replaceEntities(1) |
| 861 |
ctxt.parseDocument() |
| 862 |
doc = ctxt.doc() |
| 863 |
if doc.name != filename: |
| 864 |
print >> sys.stderr, "Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, doc.name) |
| 865 |
sys.exit(4) |
| 866 |
except: |
| 867 |
print >> sys.stderr, "Error: cannot open file '%s'." % (filename) |
| 868 |
sys.exit(1) |
| 869 |
|
| 870 |
msg.setFilename(filename) |
| 871 |
if CurrentXmlMode and origxml=='': |
| 872 |
CurrentXmlMode.preProcessXml(doc,msg) |
| 873 |
doSerialize(doc) |
| 874 |
|
| 875 |
if output == '-': |
| 876 |
out = sys.stdout |
| 877 |
else: |
| 878 |
try: |
| 879 |
out = file(output, 'w') |
| 880 |
except: |
| 881 |
print >> sys.stderr, "Error: cannot open file %s for writing." % (output) |
| 882 |
sys.exit(5) |
| 883 |
|
| 884 |
if mode != 'merge': |
| 885 |
if CurrentXmlMode: |
| 886 |
tcmsg = CurrentXmlMode.getStringForTranslators() |
| 887 |
tccom = CurrentXmlMode.getCommentForTranslators() |
| 888 |
if tcmsg: |
| 889 |
msg.outputMessage(tcmsg, 0, tccom) |
| 890 |
|
| 891 |
msg.outputAll(out) |
| 892 |
else: |
| 893 |
if CurrentXmlMode: |
| 894 |
tcmsg = CurrentXmlMode.getStringForTranslators() |
| 895 |
if tcmsg: |
| 896 |
outtxt = getTranslation(tcmsg) |
| 897 |
else: |
| 898 |
outtxt = '' |
| 899 |
CurrentXmlMode.postProcessXmlTranslation(doc, translationlanguage, outtxt) |
| 900 |
out.write(doc.serialize('utf-8', 1)) |