File indexing completed on 2024-04-21 05:44:47

0001 # -*- coding: UTF-8 -*
0002 
0003 """
0004 Various specific translation checks.
0005 
0006 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0007 @license: GPLv3
0008 """
0009 
0010 from pology import _
0011 
0012 
0013 def check_keyword_list (strict=False):
0014     """
0015     Verify that the translated keyword list has proper syntax according
0016     to the type of keyword message [hook factory].
0017 
0018     The following types of keyword messages are currently detected:
0019       - With context "Keywords" (or starts with "Keywords|"),
0020         indicates .desktop keyword list.
0021       - With context "X-KDE-Keywords" (or starts with "X-KDE-Keywords|"),
0022         indicates KDE-specific .desktop keyword list.
0023 
0024     The C{strict} parameter determines whether the C{msgstr} is checked
0025     only if the C{msgid} itself is valid (C{False}), or regardless
0026     of the validity of C{msgid} (C{True}).
0027 
0028     The checks may limit the actual valid syntax for the list type,
0029     such that some valid corner cases are not allowed.
0030     This is done when respecting a corner case would result in
0031     not catching frequently observed semantic errors.
0032 
0033     @param strict: whether to require valid C{msgstr} even if C{msgid} is not
0034     @type strict: bool
0035 
0036     @return: type V3C hook
0037     @rtype: C{(msgstr, msg, cat) -> spans}
0038     """
0039 
0040     def checkf (msgstr, msg, cat):
0041 
0042         if not strict:
0043             orig_spans = _check_keyword_list_text(msg.msgid, msg, cat)
0044             do_check = (len(orig_spans) == 0)
0045         else:
0046             do_check = True
0047 
0048         if do_check:
0049             spans = _check_keyword_list_text(msgstr, msg, cat)
0050         else:
0051             spans = []
0052 
0053         return spans
0054 
0055     return checkf
0056 
0057 
0058 def _check_keyword_list_text (text, msg, cat):
0059 
0060     spans = []
0061 
0062     ctxt = msg.msgctxt or ""
0063 
0064     if ctxt == "Keywords" or ctxt.startswith("Keywords|"):
0065         pos = text.find(",")
0066         if pos >= 0:
0067             spans.append((pos, pos + 1,
0068                           _("@info",
0069                             "Keyword list with context '%(ident)s' "
0070                             "must not contain commas.",
0071                             ident="Keywords")))
0072         if not text.endswith(";"):
0073             spans.append((len(text), len(text),
0074                           _("@info",
0075                             "Keyword list with context '%(ident)s' "
0076                             "must end with semicolon.",
0077                             ident="Keywords")))
0078 
0079     elif ctxt == "X-KDE-Keywords" or ctxt.startswith("X-KDE-Keywords|"):
0080         pos = text.find(";")
0081         if pos >= 0:
0082             spans.append((pos, pos + 1,
0083                           _("@info",
0084                             "Keyword list with context '%(ident)s' "
0085                             "must not contain semicolons.",
0086                             ident="X-KDE-Keywords")))
0087 
0088     return spans
0089 
0090