~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to cmake/codecheck/rules/translations_printf.py

  • Committer: The Widelands Bunnybot
  • Date: 2021-12-30 20:11:15 UTC
  • Revision ID: bunnybot@widelands.org-20211230201115-636kyp607bco11an
Replace Boost::Asio → Asio (#5164)

Co-authored-by: Tóth András <txa-dev@posteo.hu>

(by Noordfrees)
7841615f6a955b941058acc5d9deb98e6e1b2dc0

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
"""Checks that all translatable strings that have multiple printf placeholders
4
4
have defined those as reversible.
5
5
 
6
 
Checks that unordered, ordered and boost placeholders aren't mixed up in the same string.
 
6
Checks that unordered, ordered and wildcard placeholders aren't mixed up in the same string.
7
7
 
8
8
Checks that ngettext singular and plural strings have the same placeholders.
9
9
 
15
15
# Regex to find placeholders
16
16
FIND_UNORDERED = re.compile(r'(\%[0-9#*]*\.*[0-9#]*[a-zA-Z]{1,2})')
17
17
FIND_ORDERED = re.compile(r'\%[|]{0,1}(\d\$[0-9#*]*\.*[0-9#]*[a-zA-Z]{1,2})')
18
 
FIND_BOOST = re.compile(r'\%(\d)\%')
19
 
CLEAN_BOOST_FOR_UNORDERED = re.compile(r'(\%\d\%)')
 
18
FIND_WILDCARD = re.compile(r'\%(\d)\%')
 
19
CLEAN_WILDCARD_FOR_UNORDERED = re.compile(r'(\%\d\%)')
20
20
 
21
21
 
22
22
def FIND_UNORDERED_placeholders(sanitized_entry):
23
 
    """We need to remove boost-style matches first, because we have cases like.
 
23
    """We need to remove wildcard matches first, because we have cases like.
24
24
 
25
25
    %1%m that match both regex expressions.
26
26
    """
27
 
    for entry in CLEAN_BOOST_FOR_UNORDERED.findall(sanitized_entry):
 
27
    for entry in CLEAN_WILDCARD_FOR_UNORDERED.findall(sanitized_entry):
28
28
        sanitized_entry = sanitized_entry.replace(entry, '')
29
29
    return FIND_UNORDERED.findall(sanitized_entry)
30
30
 
37
37
        return 'Translatable string has multiple sprintf placeholders that are not ordered:'
38
38
    else:
39
39
        ordered = FIND_ORDERED.findall(sanitized_entry)
40
 
        boost = FIND_BOOST.findall(sanitized_entry)
 
40
        wildcard = FIND_WILDCARD.findall(sanitized_entry)
41
41
        if len(unordered) > 0:
42
42
            if len(ordered) > 0:
43
43
                return 'Translatable string is mixing unordered sprintf placeholders with ordered placeholders:'
44
 
            if len(boost) > 0:
45
 
                return 'Translatable string is mixing unordered sprintf placeholders with boost-style placeholders:'
46
 
        if len(ordered) > 0 and len(boost) > 0:
47
 
            return 'Translatable string is mixing ordered sprintf placeholders with boost-style placeholders:'
 
44
            if len(wildcard) > 0:
 
45
                return 'Translatable string is mixing unordered sprintf placeholders with wildcard placeholders:'
 
46
        if len(ordered) > 0 and len(wildcard) > 0:
 
47
            return 'Translatable string is mixing ordered sprintf placeholders with wildcard placeholders:'
48
48
        if len(ordered) > 0:
49
49
            for entryno, placeholder in enumerate(ordered, 1):
50
50
                if str(entryno) != placeholder[:placeholder.find('$')]:
51
51
                    return 'Translatable string has an ordered sprintf placeholder "' + placeholder + '" in position ' + str(entryno) + " - the numbers don't match:"
52
 
        if len(boost) > 0:
53
 
            for entryno, placeholder in enumerate(boost, 1):
 
52
        if len(wildcard) > 0:
 
53
            for entryno, placeholder in enumerate(wildcard, 1):
54
54
                if str(entryno) != placeholder:
55
 
                    return 'Translatable string has an ordered boost-style placeholder "' + placeholder + '" in position ' + str(entryno) + " - the numbers don't match:"
 
55
                    return 'Translatable string has an ordered wildcard placeholder "' + placeholder + '" in position ' + str(entryno) + " - the numbers don't match:"
56
56
    return ''
57
57
 
58
58
 
62
62
    sanitized_entry1 = entry1.replace('%%', '')
63
63
    sanitized_entry2 = entry2.replace('%%', '')
64
64
 
65
 
    # There is interaction between boost and unordered, so boost has to come
66
 
    # first.
67
 
    placeholders1 = FIND_BOOST.findall(sanitized_entry1)
68
 
    placeholders2 = FIND_BOOST.findall(sanitized_entry2)
 
65
    # There is interaction between wildcard and unordered, so wildcard has to come first.
 
66
    placeholders1 = FIND_WILDCARD.findall(sanitized_entry1)
 
67
    placeholders2 = FIND_WILDCARD.findall(sanitized_entry2)
69
68
    if len(placeholders1) == 0 and len(placeholders2) == 0:
70
69
        placeholders1 = FIND_ORDERED.findall(sanitized_entry1)
71
70
        placeholders2 = FIND_ORDERED.findall(sanitized_entry2)
170
169
    # Unordered with multiple
171
170
    '_("One %d and another %s")',
172
171
 
173
 
    # Mixed Boost + Ordered
 
172
    # Mixed Wildcard + Ordered
174
173
    '_("One %1$i and another %2%")',
175
174
 
176
 
    # Mixed Boost + Unordered
 
175
    # Mixed Wildcard + Unordered
177
176
    '_("One %i and another %2%")',
178
177
 
179
178
    # Mixed Ordered + Unordered
185
184
]
186
185
 
187
186
allowed = [
188
 
    # Boost followed by letter
 
187
    # Wildcard followed by letter
189
188
    'npgettext("foo", "%1%d", "%1%d", value)',
190
189
    'ngettext("%1%m", "%1%m", value)',
191
190
    'gettext("%1%s")',
192
191
    '_("%1%s")',
193
 
    # Boost
 
192
    # Wildcard
194
193
    'npgettext("foo", "%1%", "%1%", value)',
195
194
    'ngettext("%1%", "%1%", value)',
196
195
    'gettext("%1%")',
197
196
    '_("%1%")',
198
 
    # Boost with percent
 
197
    # Wildcard with percent
199
198
    'npgettext("foo", "%1%%%", "%1%%%", value)',
200
199
    'ngettext("%1%%%", "%1%%%", value)',
201
200
    'gettext("%1%%%")',
202
201
    '_("%1%%%")',
203
 
    # Boost with multiple
 
202
    # Wildcard with multiple
204
203
    'npgettext("foo", "One %1% and another %2%", "Many %1% and another %2%", value,)',
205
204
    'ngettext("One %1% and another %2%", "Many %1% and another %2%", value)',
206
205
    'gettext("One %1% and another %2%")',