Blame translation-canary/translation_canary/translated/test_markup.py

Packit b040ce
# Check translations of pango markup
Packit b040ce
#
Packit b040ce
# This will look for translatable strings that appear to contain markup and
Packit b040ce
# check that the markup in the translation matches.
Packit b040ce
#
Packit b040ce
# Copyright (C) 2015  Red Hat, Inc.
Packit b040ce
#
Packit b040ce
# This copyrighted material is made available to anyone wishing to use,
Packit b040ce
# modify, copy, or redistribute it subject to the terms and conditions of
Packit b040ce
# the GNU Lesser General Public License v.2, or (at your option) any later
Packit b040ce
# version. This program is distributed in the hope that it will be useful,
Packit b040ce
# but WITHOUT ANY WARRANTY expressed or implied, including the implied
Packit b040ce
# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
Packit b040ce
# the GNU Lesser General Public License for more details.  You should have
Packit b040ce
# received a copy of the GNU Lesser General Public License along with this
Packit b040ce
# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Packit b040ce
# Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat trademarks
Packit b040ce
# that are incorporated in the source code or documentation are not subject
Packit b040ce
# to the GNU Lesser General Public License and may only be used or
Packit b040ce
# replicated with the express permission of Red Hat, Inc.
Packit b040ce
#
Packit b040ce
# Red Hat Author(s): David Shea <dshea@redhat.com>
Packit b040ce
Packit b040ce
try:
Packit b040ce
    import polib
Packit b040ce
except ImportError:
Packit b040ce
    print("You need to install the python-polib package to read translations")
Packit b040ce
    raise
Packit b040ce
Packit b040ce
from pocketlint.pangocheck import is_markup, markup_match
Packit b040ce
import xml.etree.ElementTree as ET
Packit b040ce
Packit b040ce
def test_markup(pofile):
Packit b040ce
    po = polib.pofile(pofile)
Packit b040ce
Packit b040ce
    for entry in po.translated_entries():
Packit b040ce
        if is_markup(entry.msgid):
Packit b040ce
            # If this is a plural, check each of the plural translations
Packit b040ce
            if entry.msgid_plural:
Packit b040ce
                xlations = entry.msgstr_plural
Packit b040ce
            else:
Packit b040ce
                xlations = {None: entry.msgstr}
Packit b040ce
Packit b040ce
            for plural_id, msgstr in xlations.items():
Packit b040ce
                # Check if the markup is valid at all
Packit b040ce
                try:
Packit b040ce
                    # pylint: disable=unescaped-markup
Packit b040ce
                    ET.fromstring('<markup>%s</markup>' % msgstr)
Packit b040ce
                except ET.ParseError:
Packit b040ce
                    if entry.msgid_plural:
Packit b040ce
                        raise AssertionError("Invalid markup translation for %d translation of msgid %s\n%s" %
Packit b040ce
                                (plural_id, entry.msgid, msgstr))
Packit b040ce
                    else:
Packit b040ce
                        raise AssertionError("Invalid markup translation for msgid %s\n%s" %
Packit b040ce
                                (entry.msgid, msgstr))
Packit b040ce
Packit b040ce
                # Check if the markup has the same number and kind of tags
Packit b040ce
                if not markup_match(entry.msgid, msgstr):
Packit b040ce
                    if entry.msgid_plural:
Packit b040ce
                        raise AssertionError("Markup does not match for %d translation of msgid %s\n%s" %
Packit b040ce
                                (plural_id, entry.msgid, msgstr))
Packit b040ce
                    else:
Packit b040ce
                        raise AssertionError("Markup does not match for msgid %s\n%s" % (entry.msgid, msgstr))