i18n/check-translation.py
author Yuya Nishihara <yuya@tcha.org>
Sat, 26 Jan 2019 17:53:03 +0900
changeset 41421 f83b230b7fb3
parent 40185 19fc5a986669
child 43076 2372284d9457
permissions -rwxr-xr-x
dispatch: unify handler of IOError and OSError These exceptions were merged in Python 3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     1
#!/usr/bin/env python
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     3
# check-translation.py - check Mercurial specific translation problems
33899
078099304772 i18n: update check-translation script to pass import checker
Augie Fackler <raf@durin42.com>
parents: 33715
diff changeset
     4
from __future__ import absolute_import
078099304772 i18n: update check-translation script to pass import checker
Augie Fackler <raf@durin42.com>
parents: 33715
diff changeset
     5
078099304772 i18n: update check-translation script to pass import checker
Augie Fackler <raf@durin42.com>
parents: 33715
diff changeset
     6
import re
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     8
import polib
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    10
scanners = []
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
checkers = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    12
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    13
def scanner():
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    14
    def decorator(func):
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    15
        scanners.append(func)
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    16
        return func
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    17
    return decorator
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    18
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
    19
def levelchecker(level, msgidpat):
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
    def decorator(func):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
        if msgidpat:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
            match = re.compile(msgidpat).search
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    23
        else:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
            match = lambda msgid: True
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    25
        checkers.append((func, level))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
        func.match = match
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    27
        return func
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    28
    return decorator
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    29
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    30
def match(checker, pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
    """Examine whether POEntry "pe" is target of specified checker or not
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    32
    """
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
    if not checker.match(pe.msgid):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
        return
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
    # examine suppression by translator comment
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
    nochecker = 'no-%s-check' % checker.__name__
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
    for tc in pe.tcomment.split():
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    38
        if nochecker == tc:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
            return
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    40
    return True
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    41
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    42
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    43
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    44
def fatalchecker(msgidpat=None):
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
    45
    return levelchecker('fatal', msgidpat)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
@fatalchecker(r'\$\$')
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    48
def promptchoice(pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    49
    """Check translation of the string given to "ui.promptchoice()"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    50
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    51
    >>> pe = polib.POEntry(
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    52
    ...     msgid ='prompt$$missing &sep$$missing &amp$$followed by &none',
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    53
    ...     msgstr='prompt  missing &sep$$missing  amp$$followed by none&')
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
    >>> match(promptchoice, pe)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    55
    True
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
    56
    >>> for e in promptchoice(pe): print(e)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    57
    number of choices differs between msgid and msgstr
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    58
    msgstr has invalid choice missing '&'
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    59
    msgstr has invalid '&' followed by none
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
    """
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    61
    idchoices = [c.rstrip(' ') for c in pe.msgid.split('$$')[1:]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    62
    strchoices = [c.rstrip(' ') for c in pe.msgstr.split('$$')[1:]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    63
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    64
    if len(idchoices) != len(strchoices):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
        yield "number of choices differs between msgid and msgstr"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    66
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    67
    indices = [(c, c.find('&')) for c in strchoices]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
    if [c for c, i in indices if i == -1]:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    69
        yield "msgstr has invalid choice missing '&'"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
    if [c for c, i in indices if len(c) == i + 1]:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
        yield "msgstr has invalid '&' followed by none"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    72
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    73
deprecatedpe = None
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    74
@scanner()
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    75
def deprecatedsetup(pofile):
26852
3fb36dec1727 i18n: do not abuse msgstr of "DEPRECATED" to check for bad translation
Yuya Nishihara <yuya@tcha.org>
parents: 26838
diff changeset
    76
    pes = [p for p in pofile if p.msgid == '(DEPRECATED)' and p.msgstr]
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    77
    if len(pes):
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    78
        global deprecatedpe
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    79
        deprecatedpe = pes[0]
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    80
26837
33894facc180 i18n: fix regexp pattern to detect translation for DEPRECATED
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26277
diff changeset
    81
@fatalchecker(r'\(DEPRECATED\)')
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    82
def deprecated(pe):
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    83
    """Check for DEPRECATED
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    84
    >>> ped = polib.POEntry(
26852
3fb36dec1727 i18n: do not abuse msgstr of "DEPRECATED" to check for bad translation
Yuya Nishihara <yuya@tcha.org>
parents: 26838
diff changeset
    85
    ...     msgid = '(DEPRECATED)',
3fb36dec1727 i18n: do not abuse msgstr of "DEPRECATED" to check for bad translation
Yuya Nishihara <yuya@tcha.org>
parents: 26838
diff changeset
    86
    ...     msgstr= '(DETACERPED)')
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    87
    >>> deprecatedsetup([ped])
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    88
    >>> pe = polib.POEntry(
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    89
    ...     msgid = 'Something (DEPRECATED)',
26277
ad4d6c7aea6a tests: add more doctests for check-translation deprecated
timeless@mozdev.org
parents: 26276
diff changeset
    90
    ...     msgstr= 'something (DEPRECATED)')
ad4d6c7aea6a tests: add more doctests for check-translation deprecated
timeless@mozdev.org
parents: 26276
diff changeset
    91
    >>> match(deprecated, pe)
ad4d6c7aea6a tests: add more doctests for check-translation deprecated
timeless@mozdev.org
parents: 26276
diff changeset
    92
    True
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
    93
    >>> for e in deprecated(pe): print(e)
26277
ad4d6c7aea6a tests: add more doctests for check-translation deprecated
timeless@mozdev.org
parents: 26276
diff changeset
    94
    >>> pe = polib.POEntry(
ad4d6c7aea6a tests: add more doctests for check-translation deprecated
timeless@mozdev.org
parents: 26276
diff changeset
    95
    ...     msgid = 'Something (DEPRECATED)',
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    96
    ...     msgstr= 'something (DETACERPED)')
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    97
    >>> match(deprecated, pe)
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
    98
    True
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
    99
    >>> for e in deprecated(pe): print(e)
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   100
    >>> pe = polib.POEntry(
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   101
    ...     msgid = 'Something (DEPRECATED)',
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   102
    ...     msgstr= 'something')
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   103
    >>> match(deprecated, pe)
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   104
    True
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
   105
    >>> for e in deprecated(pe): print(e)
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   106
    msgstr inconsistently translated (DEPRECATED)
26837
33894facc180 i18n: fix regexp pattern to detect translation for DEPRECATED
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26277
diff changeset
   107
    >>> pe = polib.POEntry(
33894facc180 i18n: fix regexp pattern to detect translation for DEPRECATED
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26277
diff changeset
   108
    ...     msgid = 'Something (DEPRECATED, foo bar)',
33894facc180 i18n: fix regexp pattern to detect translation for DEPRECATED
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26277
diff changeset
   109
    ...     msgstr= 'something (DETACERPED, foo bar)')
33894facc180 i18n: fix regexp pattern to detect translation for DEPRECATED
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26277
diff changeset
   110
    >>> match(deprecated, pe)
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   111
    """
26276
93395bee98ba tests: cleanup check-translation deprecated
timeless@mozdev.org
parents: 26261
diff changeset
   112
    if not ('(DEPRECATED)' in pe.msgstr or
26838
47dd34f2e727 i18n: look translation of both "DEPRECATED" and "(DEPRECATED)" up
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26837
diff changeset
   113
            (deprecatedpe and
26276
93395bee98ba tests: cleanup check-translation deprecated
timeless@mozdev.org
parents: 26261
diff changeset
   114
             deprecatedpe.msgstr in pe.msgstr)):
93395bee98ba tests: cleanup check-translation deprecated
timeless@mozdev.org
parents: 26261
diff changeset
   115
        yield "msgstr inconsistently translated (DEPRECATED)"
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   116
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   118
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   119
def warningchecker(msgidpat=None):
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
   120
    return levelchecker('warning', msgidpat)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   121
20514
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   122
@warningchecker()
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   123
def taildoublecolons(pe):
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   124
    """Check equality of tail '::'-ness between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   125
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   126
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   127
    ...     msgid ='ends with ::',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   128
    ...     msgstr='ends with ::')
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
   129
    >>> for e in taildoublecolons(pe): print(e)
20514
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   130
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   131
    ...     msgid ='ends with ::',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   132
    ...     msgstr='ends without double-colons')
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
   133
    >>> for e in taildoublecolons(pe): print(e)
20514
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   134
    tail '::'-ness differs between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   135
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   136
    ...     msgid ='ends without double-colons',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   137
    ...     msgstr='ends with ::')
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
   138
    >>> for e in taildoublecolons(pe): print(e)
20514
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   139
    tail '::'-ness differs between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   140
    """
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   141
    if pe.msgid.endswith('::') != pe.msgstr.endswith('::'):
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   142
        yield "tail '::'-ness differs between msgid and msgstr"
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
   143
20515
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   144
@warningchecker()
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   145
def indentation(pe):
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   146
    """Check equality of initial indentation between msgid and msgstr
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   147
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   148
    This may report unexpected warning, because this doesn't aware
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   149
    the syntax of rst document and the context of msgstr.
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   150
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   151
    >>> pe = polib.POEntry(
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   152
    ...     msgid ='    indented text',
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   153
    ...     msgstr='  narrowed indentation')
33715
29238dbf718e i18n: fix check-translation.py to be less broken on Python 3
Augie Fackler <augie@google.com>
parents: 26852
diff changeset
   154
    >>> for e in indentation(pe): print(e)
20515
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   155
    initial indentation width differs betweeen msgid and msgstr
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   156
    """
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   157
    idindent = len(pe.msgid) - len(pe.msgid.lstrip())
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   158
    strindent = len(pe.msgstr) - len(pe.msgstr.lstrip())
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   159
    if idindent != strindent:
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   160
        yield "initial indentation width differs betweeen msgid and msgstr"
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   161
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   162
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   163
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   164
def check(pofile, fatal=True, warning=False):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   165
    targetlevel = { 'fatal': fatal, 'warning': warning }
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   166
    targetcheckers = [(checker, level)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   167
                      for checker, level in checkers
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   168
                      if targetlevel[level]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   169
    if not targetcheckers:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   170
        return []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   171
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   172
    detected = []
26261
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   173
    for checker in scanners:
8fb92ff63ccf tests: check for inconsistently translated DEPRECATED
timeless@mozdev.org
parents: 22203
diff changeset
   174
        checker(pofile)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   175
    for pe in pofile.translated_entries():
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   176
        errors = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   177
        for checker, level in targetcheckers:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   178
            if match(checker, pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   179
                errors.extend((level, checker.__name__, error)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   180
                              for error in checker(pe))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   181
        if errors:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   182
            detected.append((pe, errors))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   183
    return detected
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   184
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   185
########################################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   186
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   187
if __name__ == "__main__":
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   188
    import sys
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   189
    import optparse
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   190
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   191
    optparser = optparse.OptionParser("""%prog [options] pofile ...
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   192
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   193
This checks Mercurial specific translation problems in specified
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   194
'*.po' files.
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   195
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   196
Each detected problems are shown in the format below::
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   197
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   198
    filename:linenum:type(checker): problem detail .....
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   199
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   200
"type" is "fatal" or "warning". "checker" is the name of the function
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   201
detecting corresponded error.
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   202
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   203
Checking by checker "foo" on the specific msgstr can be suppressed by
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   204
the "translator comment" like below. Multiple "no-xxxx-check" should
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   205
be separated by whitespaces::
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   206
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   207
    # no-foo-check
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   208
    msgid = "....."
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   209
    msgstr = "....."
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   210
""")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   211
    optparser.add_option("", "--warning",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   212
                         help="show also warning level problems",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   213
                         action="store_true")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   214
    optparser.add_option("", "--doctest",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   215
                         help="run doctest of this tool, instead of check",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   216
                         action="store_true")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   217
    (options, args) = optparser.parse_args()
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   218
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   219
    if options.doctest:
20164
1ddf4409229f tests: fix missing import in check-translations
Matt Mackall <mpm@selenic.com>
parents: 20158
diff changeset
   220
        import os
20158
209e04a06467 tests: fix Mac doctest escape code garbage for check-translations
Matt Mackall <mpm@selenic.com>
parents: 20152
diff changeset
   221
        if 'TERM' in os.environ:
209e04a06467 tests: fix Mac doctest escape code garbage for check-translations
Matt Mackall <mpm@selenic.com>
parents: 20152
diff changeset
   222
            del os.environ['TERM']
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   223
        import doctest
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   224
        failures, tests = doctest.testmod()
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   225
        sys.exit(failures and 1 or 0)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   226
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   227
    detected = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   228
    warning = options.warning
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   229
    for f in args:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   230
        detected.extend((f, pe, errors)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   231
                        for pe, errors in check(polib.pofile(f),
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   232
                                                warning=warning))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   233
    if detected:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   234
        for f, pe, errors in detected:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   235
            for level, checker, error in errors:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   236
                sys.stderr.write('%s:%d:%s(%s): %s\n'
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   237
                                 % (f, pe.linenum, level, checker, error))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   238
        sys.exit(1)