X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=npybabel.py;h=14ac115e338463f6cd6fa5460c9caea5ae9e37bb;hb=a05b3f3a2ec29fbb3c1d5f6b7f64004290b6c62d;hp=b2888cd4a2cc35092b08889ab389e6e0cd49ef86;hpb=7ebc5bdba308c376fcf53f2d6da7bc7beb41be2d;p=odoo%2Fodoo.git diff --git a/npybabel.py b/npybabel.py index b2888cd..14ac115 100755 --- a/npybabel.py +++ b/npybabel.py @@ -17,6 +17,10 @@ XMLJS_EXPR = re.compile(r"""(?:\_t *\( *((?:"(?:[^"\\]|\\.)*")|(?:'(?:[^'\\]|\\. TRANSLATION_FLAG_COMMENT = "openerp-web" +# List of etree._Element subclasses that we choose to ignore when parsing XML. +# We include the *Base ones just in case, currently they seem to be subclasses of the _* ones. +SKIPPED_ELEMENT_TYPES = (elt._Comment, elt._ProcessingInstruction, elt.CommentBase, elt.PIBase) + def extract_xmljs(fileobj, keywords, comment_tags, options): """Extract messages from Javascript code embedded into XML documents. This complements the ``extract_javascript`` extractor which works @@ -34,6 +38,34 @@ def extract_xmljs(fileobj, keywords, comment_tags, options): tuples :rtype: ``iterator`` """ + assert False, """ the XMLJS extractor does not work and was removed: + + * Babel apparently does not accept two extractors for the same set of files + so it would not run the xmljs extractor at all, extraction of JS stuff + needs to be done from the XML extractor + * The regex above fails up if there are back-slashed quotes within the + translatable string (the string marked with _t), it just won't match the + string + * While extraction succeeds on XML entities (e.g. "), translation + matching will fail if those entities are kept in the PO msgid as the + XML parser will get an un-escaped string, without those entities (so a + text extractor will extract ``Found match "%s"``, but the msgid + of the PO file must be ``Found match "%s"`` or the translation will fail + * single-quoted strings are not valid JSON string, so single-quoted strings + matched by the regex (likely since XML attributes are double-quoted, + single quotes within them don't have to be escaped) will blow up when + json-parsed for their content + + I think that's about it. + + If this extractor is reimplemented, it should be integrated into + extract_qweb, either in the current pass (probably not a good idea) or as + a separate pass using iterparse, matching either elements with t-js or + some other kinds of t-* directives (@t-esc, @t-raw, @t-att, others?), + shove the attribute content into a StringIO and pass *that* to Babel's + own extract_javascript; then add a line offset in order to yield the + correct line number. + """ content = fileobj.read() found = XMLJS_EXPR.finditer(content) index = 0 @@ -66,12 +98,18 @@ def extract_qweb(fileobj, keywords, comment_tags, options): if len(text) > 1: # Avoid mono-char tokens like ':' ',' etc. result.append((lineno, None, text, [TRANSLATION_FLAG_COMMENT])) + # not using elementTree.iterparse because we need to skip sub-trees in case + # the ancestor element had a reason to be skipped def iter_elements(current_element): for el in current_element: + if isinstance(el, SKIPPED_ELEMENT_TYPES): continue if "t-js" not in el.attrib and \ not ("t-jquery" in el.attrib and "t-operation" not in el.attrib) and \ not ("t-translation" in el.attrib and el.attrib["t-translation"].strip() == "off"): handle_text(el.text, el.sourceline) + for att in ('title', 'alt', 'label', 'placeholder'): + if att in el.attrib: + handle_text(el.attrib[att], el.sourceline) iter_elements(el) handle_text(el.tail, el.sourceline)