Repository: dogecoin/dogecoin
File: contrib/devtools/check-translations.py (Python)

Code before:
        parser = ET.XMLParser(encoding='utf-8')
        with open(filepath, 'rb') as f:
            data = f.read()

        # find control characters; otherwise the XML parser will fail
        if FIX_RE.match(data):
            print(f'{{ filename }}: Found control characters, this needs to be fixed.')
            have_errors = True
            continue

        tree = ET.parse(io.BytesIO(data), parser=parser)

        # iterate over all messages in file
        root = tree.getroot()
        for context in root.findall('context'):
            for message in context.findall('message'):
                numerus = message.get('numerus') == 'yes'
                source = message.find('source').text
                translation_node = message.find('translation')
                # pick all numerusforms
                if numerus:
                    translations = [i.text for i in translation_node.findall('numerusform')]
                    if len(translations) == 0:
                        print(f'Numerus message without numerusform translations, needs fix.')
                        have_errors = True
                    if not translation_node.text.isspace():
                        print(f'Numerus message contains extra text {translation_node.text}, needs fix.')
                        have_errors = True
                    for child in list(translation_node.iter())[1:]: # exclude root
                        if child.tag != 'numerusform':
                            print('Numerus message contains extra child node, needs fix.')
                            have_errors = True
                            break
                else:
                    translations = [translation_node.text]
                    if len(list(translation_node.iter())) != 1:
                        print('Non-numerus message contains child node in translation, needs fix.')
                        have_errors = True
                    
                for translation in translations:
                    if translation is None:
                        continue
                    errors = []
                    valid = check_format_specifiers(source, translation, errors, numerus)

                    for error in errors:
                        print('%s: %s' % (filename, error))

                    if not valid:
                        have_errors = True


Review comment (suggestion):
also needs to check `translation_node.text is not None`, or this can error out when there are child elements but no text.

Code after:
        parser = ET.XMLParser(encoding='utf-8')
        with open(filepath, 'rb') as f:
            data = f.read()

        # find control characters; otherwise the XML parser will fail
        if FIX_RE.match(data):
            print(f'{{ filename }}: Found control characters, this needs to be fixed.')
            have_errors = True
            continue

        tree = ET.parse(io.BytesIO(data), parser=parser)

        # iterate over all messages in file
        root = tree.getroot()
        for context in root.findall('context'):
            for message in context.findall('message'):
                numerus = message.get('numerus') == 'yes'
                source = message.find('source').text
                translation_node = message.find('translation')
                # pick all numerusforms
                if numerus:
                    translations = [i.text for i in translation_node.findall('numerusform')]
                    if len(translations) == 0:
                        print(f'Numerus message without numerusform translations, needs fix.', sanitize_string(source))
                        have_errors = True
                    if translation_node.text is not None and not translation_node.text.isspace():
                        print(f'Numerus message contains extra text {translation_node.text}, needs fix.')
                        have_errors = True
                    for child in list(translation_node.iter())[1:]: # exclude root
                        if child.tag != 'numerusform':
                            print('Numerus message contains extra child node, needs fix.', sanitize_string(source))
                            have_errors = True
                            break
                else:
                    translations = [translation_node.text]
                    if len(list(translation_node.iter())) != 1:
                        print('Non-numerus message contains child node in translation, needs fix.', sanitize_string(source))
                        have_errors = True
                    
                for translation in translations:
                    if translation is None:
                        continue
                    errors = []
                    valid = check_format_specifiers(source, translation, errors, numerus)

                    for error in errors:
                        print('%s: %s' % (filename, error))

                    if not valid:
                        have_errors = True