| 1 | # Check translations of pango markup |
| 2 | # |
| 3 | # This will look for translatable strings that appear to contain markup and |
| 4 | # check that the markup in the translation matches. |
| 5 | # |
| 6 | # Copyright (C) 2015 Red Hat, Inc. |
| 7 | # |
| 8 | # This copyrighted material is made available to anyone wishing to use, |
| 9 | # modify, copy, or redistribute it subject to the terms and conditions of |
| 10 | # the GNU Lesser General Public License v.2, or (at your option) any later |
| 11 | # version. This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY expressed or implied, including the implied |
| 13 | # warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | # the GNU Lesser General Public License for more details. You should have |
| 15 | # received a copy of the GNU Lesser General Public License along with this |
| 16 | # program; if not, write to the Free Software Foundation, Inc., 51 Franklin |
| 17 | # Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks |
| 18 | # that are incorporated in the source code or documentation are not subject |
| 19 | # to the GNU Lesser General Public License and may only be used or |
| 20 | # replicated with the express permission of Red Hat, Inc. |
| 21 | # |
| 22 | # Red Hat Author(s): David Shea <[email protected]> |
| 23 | |
| 24 | try: |
| 25 | import polib |
| 26 | except ImportError: |
| 27 | print("You need to install the python-polib package to read translations") |
| 28 | raise |
| 29 | |
| 30 | from pocketlint.pangocheck import is_markup, markup_match |
| 31 | import xml.etree.ElementTree as ET |
| 32 | |
| 33 | |
| 34 | def test_markup(pofile): |
| 35 | po = polib.pofile(pofile) |
| 36 | |
| 37 | for entry in po.translated_entries(): |
| 38 | if is_markup(entry.msgid): |
| 39 | # If this is a plural, check each of the plural translations |
| 40 | if entry.msgid_plural: |
| 41 | xlations = entry.msgstr_plural |
| 42 | else: |
| 43 | xlations = {None: entry.msgstr} |
| 44 | |
| 45 | for plural_id, msgstr in xlations.items(): |
| 46 | # Check if the markup is valid at all |
| 47 | try: |
| 48 | # pylint: disable=unescaped-markup |
| 49 | ET.fromstring('<markup>%s</markup>' % msgstr) |
| 50 | except ET.ParseError as e: |
| 51 | if entry.msgid_plural: |
| 52 | raise AssertionError( |
| 53 | "Invalid markup translation for {} translation of msgid {}\n{}". |
| 54 | format(plural_id, entry.msgid, msgstr)) from e |
| 55 | else: |
| 56 | raise AssertionError( |
| 57 | "Invalid markup translation for msgid {}\n{}". |
| 58 | format(entry.msgid, msgstr)) from e |
| 59 | |
| 60 | # Check if the markup has the same number and kind of tags |
| 61 | if not markup_match(entry.msgid, msgstr): |
| 62 | if entry.msgid_plural: |
| 63 | raise AssertionError( |
| 64 | "Markup does not match for {} translation of msgid {}\n{}". |
| 65 | format(plural_id, entry.msgid, msgstr)) |
| 66 | else: |
| 67 | raise AssertionError( |
| 68 | "Markup does not match for msgid {}\n{}". |
| 69 | format(entry.msgid, msgstr)) |