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
|
__all__ = [ "load_xtb" ]
import logging
from .utils import OrderedXMLParser, ET
class UnknownTag(ValueError): pass
def load_xtb(lang, txln_info, grd_filename, xtb_filename, conditions):
if "content/third_party/launchpad_translations/" in xtb_filename:
xtb_filename = xtb_filename.replace("content/third_party/launchpad_translations/", "third_party/launchpad_translations/")
logging.debug("Parsing %s because referenced by %s", xtb_filename, grd_filename)
tree = ET.parse(xtb_filename, parser=OrderedXMLParser())
root = tree.getroot()
parsextb_walker(lang, txln_info, grd_filename, xtb_filename, root, conditions)
def parsextb_translation(lang, txln_info, grd_filename, xtb_filename, node, conditions):
"""
>>> parsextb_translation('be', dict(), "grd", "xtb", ET.fromstring('<translation id="3848258323044014972"> n <ph name="PAGE_TITLE"/> – Chromium</translation>'), ())
{'3848258323044014972': {'langs': {'be': [('n <ph name="PAGE_TITLE" /> – Chromium', 'XTB', 'grd', 'xtb', ())]}}}
>>> parsextb_translation('zh-TW', dict(), "grd", "xtb", ET.fromstring('<translation id="3909791450649380159">剪下(&T)</translation>'), ())
{'3909791450649380159': {'langs': {'zh-TW': [('剪下(&T)', 'XTB', 'grd', 'xtb', ())]}}}
>>> parsextb_translation('zh-TW', dict(), "grd", "xtb", ET.fromstring('<translation id="3828924085048779000">通關密語欄位不得留空。</translation>'), ())
{'3828924085048779000': {'langs': {'zh-TW': [('通關密語欄位不得留空。', 'XTB', 'grd', 'xtb', ())]}}}
"""
tid = node.attrib["id"]
try:
tid = str(int(node.attrib["id"])) # raise errors when not a number! Hacky.
except ValueError:
logging.debug("Element %s in %s has a weird id %s.", node.tag, xtb_filename, node.attrib["id"])
text_parts = list()
if node.text:
text_parts.append(node.text)
for child in node:
if child.text:
text_parts.append(child.text)
if child.tag == "ph":
text_parts.append('<ph name="')
text_parts.append(child.attrib["name"])
text_parts.append('" />')
if child.tail:
text_parts.append(child.tail)
text = "".join(text_parts).strip()
if not text:
# No translation for this. That's okay!
return
if tid not in txln_info:
txln_info[tid] = dict()
if "langs" not in txln_info[tid]:
txln_info[tid]["langs"] = dict()
d = txln_info[tid]["langs"]
if lang not in d:
d[lang] = list()
# Store the filename and the source.
fileinfo = (text.strip(), "XTB", grd_filename, xtb_filename, conditions)
if fileinfo not in d[lang]:
d[lang].append(fileinfo) # Can also append data from PO later. No discarding.
return txln_info
def parsextb_walker(lang, txln_info, grd_filename, xtb_filename, node, conditions):
"""Handler of dumb nodes that we don't really care about except as
definining structure. This implements all the logic of following the
GRD_STRUCTURE."""
options = XTB_STRUCTURE[node.tag]
if options is None:
return
for child in node:
if child.tag not in options:
logging.error("Unknown tag %s in %s in %s", child, node, xtb_filename)
continue
next_step = options[child.tag]
if isinstance(next_step, str):
parsextb_walker(lang, txln_info, grd_filename, xtb_filename, child, conditions)
elif next_step is None:
continue
else:
next_step(lang, txln_info, grd_filename, xtb_filename, child, conditions)
def parsextb_if(lang, txln_info, grd_filename, xtb_filename, node, conditions):
condition = node.attrib["expr"]
parsextb_walker(lang, txln_info, grd_filename, xtb_filename, node, conditions+(condition,))
XTB_STRUCTURE = {
"translationbundle": { "translation": parsextb_translation, "if": parsextb_if },
"if": { "translation": parsextb_translation },
}
|