File indexing completed on 2024-10-27 08:25:08

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Resolve XML entities in translation.
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.entities import read_entities
0014 from pology.msgreport import warning_on_msg
0015 from pology.report import report, format_item_list
0016 from pology.resolve import resolve_entities
0017 from pology.sieve import add_param_entdef
0018 
0019 
0020 def setup_sieve (p):
0021 
0022     p.set_desc(_("@info sieve discription",
0023     "Resolve XML entities in translation."
0024     ))
0025 
0026     add_param_entdef(p)
0027     p.add_param("ignore", str, seplist=True,
0028                 metavar=_("@info sieve parameter value placeholder",
0029                           "ENTITY1,..."),
0030                 desc=_("@info sieve parameter discription",
0031     "Comma-separated list of entity names to ignore."
0032     ))
0033 
0034 
0035 class Sieve (object):
0036 
0037     def __init__ (self, params):
0038 
0039         self.entity_files = params.entdef or []
0040 
0041         self.ignored_entities = ["lt", "gt", "apos", "quot", "amp"]
0042         if params.ignore:
0043             self.ignored_entities.extend(params.ignore)
0044 
0045         # Read entity definitions.
0046         self.entities = read_entities(self.entity_files)
0047 
0048         self.nresolved = 0
0049 
0050 
0051     def process (self, msg, cat):
0052 
0053         for i in range(len(msg.msgstr)):
0054             msg.msgstr[i], resolved, unknown = \
0055                 resolve_entities(msg.msgstr[i],
0056                                  self.entities, self.ignored_entities,
0057                                  cat.filename)
0058             self.nresolved += len(resolved)
0059             if unknown:
0060                 warning_on_msg(_("@info",
0061                                  "Unknown entities in translation: "
0062                                  "%(entlist)s.",
0063                                  entlist=format_item_list(unknown)),
0064                                msg, cat)
0065 
0066 
0067     def finalize (self):
0068 
0069         if self.nresolved > 0:
0070             msg = n_("@info:progress",
0071                      "Resolved %(num)d entity in translation.",
0072                      "Resolved %(num)d entities in translation.",
0073                      num=self.nresolved)
0074             report("===== " + msg)
0075