| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# check-translation.py - check Mercurial specific translation problems |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
import polib |
| 10 |
|
| 11 |
scanners = [] |
| 12 |
checkers = [] |
| 13 |
|
| 14 |
|
| 15 |
def scanner(): |
| 16 |
def decorator(func): |
| 17 |
scanners.append(func) |
| 18 |
return func |
| 19 |
|
| 20 |
return decorator |
| 21 |
|
| 22 |
|
| 23 |
def levelchecker(level, msgidpat): |
| 24 |
def decorator(func): |
| 25 |
if msgidpat: |
| 26 |
match = re.compile(msgidpat).search |
| 27 |
else: |
| 28 |
match = lambda msgid: True |
| 29 |
checkers.append((func, level)) |
| 30 |
func.match = match |
| 31 |
return func |
| 32 |
|
| 33 |
return decorator |
| 34 |
|
| 35 |
|
| 36 |
def match(checker, pe): |
| 37 |
"""Examine whether POEntry "pe" is target of specified checker or not""" |
| 38 |
if not checker.match(pe.msgid): |
| 39 |
return |
| 40 |
# examine suppression by translator comment |
| 41 |
nochecker = 'no-%s-check' % checker.__name__ |
| 42 |
for tc in pe.tcomment.split(): |
| 43 |
if nochecker == tc: |
| 44 |
return |
| 45 |
return True |
| 46 |
|
| 47 |
|
| 48 |
#################### |
| 49 |
|
| 50 |
|
| 51 |
def fatalchecker(msgidpat=None): |
| 52 |
return levelchecker('fatal', msgidpat) |
| 53 |
|
| 54 |
|
| 55 |
@fatalchecker(r'\$\$') |
| 56 |
def promptchoice(pe): |
| 57 |
"""Check translation of the string given to "ui.promptchoice()" |
| 58 |
|
| 59 |
>>> pe = polib.POEntry( |
| 60 |
... msgid ='prompt$$missing &sep$$missing &$$followed by &none', |
| 61 |
... msgstr='prompt missing &sep$$missing amp$$followed by none&') |
| 62 |
>>> match(promptchoice, pe) |
| 63 |
True |
| 64 |
>>> for e in promptchoice(pe): print(e) |
| 65 |
number of choices differs between msgid and msgstr |
| 66 |
msgstr has invalid choice missing '&' |
| 67 |
msgstr has invalid '&' followed by none |
| 68 |
""" |
| 69 |
idchoices = [c.rstrip(' ') for c in pe.msgid.split('$$')[1:]] |
| 70 |
strchoices = [c.rstrip(' ') for c in pe.msgstr.split('$$')[1:]] |
| 71 |
|
| 72 |
if len(idchoices) != len(strchoices): |
| 73 |
yield "number of choices differs between msgid and msgstr" |
| 74 |
|
| 75 |
indices = [(c, c.find('&')) for c in strchoices] |
| 76 |
if [c for c, i in indices if i == -1]: |
| 77 |
yield "msgstr has invalid choice missing '&'" |
| 78 |
if [c for c, i in indices if len(c) == i + 1]: |
| 79 |
yield "msgstr has invalid '&' followed by none" |
| 80 |
|
| 81 |
|
| 82 |
deprecatedpe = None |
| 83 |
|
| 84 |
|
| 85 |
@scanner() |
| 86 |
def deprecatedsetup(pofile): |
| 87 |
pes = [p for p in pofile if p.msgid == '(DEPRECATED)' and p.msgstr] |
| 88 |
if len(pes): |
| 89 |
global deprecatedpe |
| 90 |
deprecatedpe = pes[0] |
| 91 |
|
| 92 |
|
| 93 |
@fatalchecker(r'\(DEPRECATED\)') |
| 94 |
def deprecated(pe): |
| 95 |
"""Check for DEPRECATED |
| 96 |
>>> ped = polib.POEntry( |
| 97 |
... msgid = '(DEPRECATED)', |
| 98 |
... msgstr= '(DETACERPED)') |
| 99 |
>>> deprecatedsetup([ped]) |
| 100 |
>>> pe = polib.POEntry( |
| 101 |
... msgid = 'Something (DEPRECATED)', |
| 102 |
... msgstr= 'something (DEPRECATED)') |
| 103 |
>>> match(deprecated, pe) |
| 104 |
True |
| 105 |
>>> for e in deprecated(pe): print(e) |
| 106 |
>>> pe = polib.POEntry( |
| 107 |
... msgid = 'Something (DEPRECATED)', |
| 108 |
... msgstr= 'something (DETACERPED)') |
| 109 |
>>> match(deprecated, pe) |
| 110 |
True |
| 111 |
>>> for e in deprecated(pe): print(e) |
| 112 |
>>> pe = polib.POEntry( |
| 113 |
... msgid = 'Something (DEPRECATED)', |
| 114 |
... msgstr= 'something') |
| 115 |
>>> match(deprecated, pe) |
| 116 |
True |
| 117 |
>>> for e in deprecated(pe): print(e) |
| 118 |
msgstr inconsistently translated (DEPRECATED) |
| 119 |
>>> pe = polib.POEntry( |
| 120 |
... msgid = 'Something (DEPRECATED, foo bar)', |
| 121 |
... msgstr= 'something (DETACERPED, foo bar)') |
| 122 |
>>> match(deprecated, pe) |
| 123 |
""" |
| 124 |
if not ( |
| 125 |
'(DEPRECATED)' in pe.msgstr |
| 126 |
or (deprecatedpe and deprecatedpe.msgstr in pe.msgstr) |
| 127 |
): |
| 128 |
yield "msgstr inconsistently translated (DEPRECATED)" |
| 129 |
|
| 130 |
|
| 131 |
#################### |
| 132 |
|
| 133 |
|
| 134 |
def warningchecker(msgidpat=None): |
| 135 |
return levelchecker('warning', msgidpat) |
| 136 |
|
| 137 |
|
| 138 |
@warningchecker() |
| 139 |
def taildoublecolons(pe): |
| 140 |
"""Check equality of tail '::'-ness between msgid and msgstr |
| 141 |
|
| 142 |
>>> pe = polib.POEntry( |
| 143 |
... msgid ='ends with ::', |
| 144 |
... msgstr='ends with ::') |
| 145 |
>>> for e in taildoublecolons(pe): print(e) |
| 146 |
>>> pe = polib.POEntry( |
| 147 |
... msgid ='ends with ::', |
| 148 |
... msgstr='ends without double-colons') |
| 149 |
>>> for e in taildoublecolons(pe): print(e) |
| 150 |
tail '::'-ness differs between msgid and msgstr |
| 151 |
>>> pe = polib.POEntry( |
| 152 |
... msgid ='ends without double-colons', |
| 153 |
... msgstr='ends with ::') |
| 154 |
>>> for e in taildoublecolons(pe): print(e) |
| 155 |
tail '::'-ness differs between msgid and msgstr |
| 156 |
""" |
| 157 |
if pe.msgid.endswith('::') != pe.msgstr.endswith('::'): |
| 158 |
yield "tail '::'-ness differs between msgid and msgstr" |
| 159 |
|
| 160 |
|
| 161 |
@warningchecker() |
| 162 |
def indentation(pe): |
| 163 |
"""Check equality of initial indentation between msgid and msgstr |
| 164 |
|
| 165 |
This may report unexpected warning, because this doesn't aware |
| 166 |
the syntax of rst document and the context of msgstr. |
| 167 |
|
| 168 |
>>> pe = polib.POEntry( |
| 169 |
... msgid =' indented text', |
| 170 |
... msgstr=' narrowed indentation') |
| 171 |
>>> for e in indentation(pe): print(e) |
| 172 |
initial indentation width differs betweeen msgid and msgstr |
| 173 |
""" |
| 174 |
idindent = len(pe.msgid) - len(pe.msgid.lstrip()) |
| 175 |
strindent = len(pe.msgstr) - len(pe.msgstr.lstrip()) |
| 176 |
if idindent != strindent: |
| 177 |
yield "initial indentation width differs betweeen msgid and msgstr" |
| 178 |
|
| 179 |
|
| 180 |
#################### |
| 181 |
|
| 182 |
|
| 183 |
def check(pofile, fatal=True, warning=False): |
| 184 |
targetlevel = {'fatal': fatal, 'warning': warning} |
| 185 |
targetcheckers = [ |
| 186 |
(checker, level) for checker, level in checkers if targetlevel[level] |
| 187 |
] |
| 188 |
if not targetcheckers: |
| 189 |
return [] |
| 190 |
|
| 191 |
detected = [] |
| 192 |
for checker in scanners: |
| 193 |
checker(pofile) |
| 194 |
for pe in pofile.translated_entries(): |
| 195 |
errors = [] |
| 196 |
for checker, level in targetcheckers: |
| 197 |
if match(checker, pe): |
| 198 |
errors.extend( |
| 199 |
(level, checker.__name__, error) for error in checker(pe) |
| 200 |
) |
| 201 |
if errors: |
| 202 |
detected.append((pe, errors)) |
| 203 |
return detected |
| 204 |
|
| 205 |
|
| 206 |
######################################## |
| 207 |
|
| 208 |
if __name__ == "__main__": |
| 209 |
import sys |
| 210 |
import optparse |
| 211 |
|
| 212 |
optparser = optparse.OptionParser( |
| 213 |
"""%prog [options] pofile ... |
| 214 |
|
| 215 |
This checks Mercurial specific translation problems in specified |
| 216 |
'*.po' files. |
| 217 |
|
| 218 |
Each detected problems are shown in the format below:: |
| 219 |
|
| 220 |
filename:linenum:type(checker): problem detail ..... |
| 221 |
|
| 222 |
"type" is "fatal" or "warning". "checker" is the name of the function |
| 223 |
detecting corresponded error. |
| 224 |
|
| 225 |
Checking by checker "foo" on the specific msgstr can be suppressed by |
| 226 |
the "translator comment" like below. Multiple "no-xxxx-check" should |
| 227 |
be separated by whitespaces:: |
| 228 |
|
| 229 |
# no-foo-check |
| 230 |
msgid = "....." |
| 231 |
msgstr = "....." |
| 232 |
""" |
| 233 |
) |
| 234 |
optparser.add_option( |
| 235 |
"", |
| 236 |
"--warning", |
| 237 |
help="show also warning level problems", |
| 238 |
action="store_true", |
| 239 |
) |
| 240 |
optparser.add_option( |
| 241 |
"", |
| 242 |
"--doctest", |
| 243 |
help="run doctest of this tool, instead of check", |
| 244 |
action="store_true", |
| 245 |
) |
| 246 |
(options, args) = optparser.parse_args() |
| 247 |
|
| 248 |
if options.doctest: |
| 249 |
import os |
| 250 |
|
| 251 |
if 'TERM' in os.environ: |
| 252 |
del os.environ['TERM'] |
| 253 |
import doctest |
| 254 |
|
| 255 |
failures, tests = doctest.testmod() |
| 256 |
sys.exit(failures and 1 or 0) |
| 257 |
|
| 258 |
detected = [] |
| 259 |
warning = options.warning |
| 260 |
for f in args: |
| 261 |
detected.extend( |
| 262 |
(f, pe, errors) |
| 263 |
for pe, errors in check(polib.pofile(f), warning=warning) |
| 264 |
) |
| 265 |
if detected: |
| 266 |
for f, pe, errors in detected: |
| 267 |
for level, checker, error in errors: |
| 268 |
sys.stderr.write( |
| 269 |
'%s:%d:%s(%s): %s\n' |
| 270 |
% (f, pe.linenum, level, checker, error) |
| 271 |
) |
| 272 |
sys.exit(1) |