File indexing completed on 2024-03-24 05:47:43

0001 # -*- coding: utf-8 -*-
0002 
0003 """
0004 Check native KDE4 PO files for various problems.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0009 @license: GPLv3
0010 """
0011 
0012 from pology import _, n_
0013 from pology.markup import flag_no_check_markup
0014 from pology.markup import validate_kde4_l1
0015 from pology.msgreport import report_on_msg, report_on_msg_hl
0016 from pology.msgreport import report_msg_to_lokalize
0017 from pology.report import report
0018 from pology.sieve import add_param_poeditors
0019 from pology.sieve import parse_sieve_flags
0020 
0021 
0022 def setup_sieve (p):
0023 
0024     p.set_desc(_("@info sieve discription",
0025     "Check native KDE4 PO files for various problems."
0026     ))
0027     p.add_param("strict", bool, defval=False,
0028                 desc=_("@info sieve parameter discription",
0029     "Check for problems in translation regardless of whether the original "
0030     "itself is free of problems (default is to check translation only if "
0031     "the original has no problems)."
0032     ))
0033     add_param_poeditors(p)
0034 
0035 
0036 class Sieve (object):
0037 
0038     def __init__ (self, params):
0039 
0040         self.strict = params.strict
0041 
0042         self.lokalize = params.lokalize
0043 
0044         # Indicators to the caller:
0045         self.caller_sync = False # no need to sync catalogs to the caller
0046         self.caller_monitored = False # no need for monitored messages
0047 
0048         self.nproblems = 0
0049 
0050 
0051     def process (self, msg, cat):
0052 
0053         # Check only translated messages.
0054         if not msg.translated:
0055             return
0056 
0057         # Do not check messages when told so.
0058         if flag_no_check_markup in parse_sieve_flags(msg):
0059             return
0060 
0061         # In in non-strict mode, check XML of translation only if the
0062         # original itself is valid XML.
0063         if not self.strict:
0064             if (   validate_kde4_l1(msg.msgid, ents={})
0065                 or validate_kde4_l1(msg.msgid_plural or "", ents={})
0066             ):
0067                 return
0068 
0069         highlight = []
0070         for i in range(len(msg.msgstr)):
0071             spans = validate_kde4_l1(msg.msgstr[i], ents={})
0072             if spans:
0073                 self.nproblems += 1
0074                 highlight.append(("msgstr", i, spans, msg.msgstr[i]))
0075 
0076         if highlight:
0077             report_on_msg_hl(highlight, msg, cat)
0078             if self.lokalize:
0079                 report_msg_to_lokalize(msg, cat, highlight)
0080 
0081 
0082     def finalize (self):
0083 
0084         if self.nproblems > 0:
0085             if not self.strict:
0086                 msg = n_("@info:progress",
0087                          "Found %(num)d problem in KDE4 translations.",
0088                          "Found %(num)d problems in KDE4 translations.",
0089                          num=self.nproblems)
0090             else:
0091                 msg = n_("@info:progress",
0092                          "Found %(num)d problem in KDE4 translations "
0093                          "(strict mode).",
0094                          "Found %(num)d problems in KDE4 translations "
0095                          "(strict mode).",
0096                          num=self.nproblems)
0097             report("===== " + msg)
0098