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
|
import re
def gettext(contents):
# replace ``_translate("context", `` by ``_(``
contents = re.sub(
r"\n", ('\n_ = __import__(__package__.split(".", 1)[0])._\n'), contents, 1
)
contents = re.sub(
r"\n\s+_translate = QtCore\.QCoreApplication\.translate\n", "\n", contents
)
def repl(m):
gd = m.groupdict()
comment = "{ws}# i18n: Widget: “{widget}”".format(**gd)
field = gd["field"]
if field:
field = " ".join(
word.lower() for word in re.split(r"([A-Z][a-z_0-9]+)", field) if word
)
comment += ", {field}".format(field=field)
comment += "."
gd["pre2"] = gd["pre2"] or ""
return "{comment}\n{ws}{pre1}{pre2}_(".format(comment=comment, **gd)
contents = re.sub(
(
r"(?P<ws> *)(?P<pre1>.*?)(?P<pre2>\.set(?P<field>\w+)\()?"
r'\b_translate\("(?P<widget>.*)",\s'
),
repl,
contents,
)
assert re.search(r"\b_translate\(", contents) is None
return contents
def no_autoconnection(contents):
# remove calls to ``QtCore.QMetaObject.connectSlotsByName``
contents = re.sub(
r"\n\s+QtCore\.QMetaObject\.connectSlotsByName\(\w+\)\n", "\n", contents
)
return contents
|