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

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