Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(76)  |  Call(72)  |  Derive(0)  |  Import(4)
Extract messages from JavaScript source code.

:param fileobj: the seekable, file-like object the messages should be
                extracted from
:param keywords: a list of keywords (i.e. function names) that should be
                 recognized as translation functions
:param comment_tags: a list of translator tags to search for and include
                     in the results
:param options: a dictionary of additional options (optional)

        def extract_javascript(fileobj, keywords, comment_tags, options):
    """Extract messages from JavaScript source code.

    :param fileobj: the seekable, file-like object the messages should be
                    extracted from
    :param keywords: a list of keywords (i.e. function names) that should be
                     recognized as translation functions
    :param comment_tags: a list of translator tags to search for and include
                         in the results
    :param options: a dictionary of additional options (optional)
    """
    from babel.messages.jslexer import tokenize, unquote_string
    funcname = message_lineno = None
    messages = []
    last_argument = None
    translator_comments = []
    concatenate_next = False
    encoding = options.get('encoding', 'utf-8')
    last_token = None
    call_stack = -1

    for token in tokenize(fileobj.read().decode(encoding)):
        if token.type == 'operator' and token.value == '(':
            if funcname:
                message_lineno = token.lineno
                call_stack += 1

        elif call_stack == -1 and token.type == 'linecomment':
            value = token.value[2:].strip()
            if translator_comments and \
               translator_comments[-1][0] == token.lineno - 1:
                translator_comments.append((token.lineno, value))
                continue

            for comment_tag in comment_tags:
                if value.startswith(comment_tag):
                    translator_comments.append((token.lineno, value.strip()))
                    break

        elif token.type == 'multilinecomment':
            # only one multi-line comment may preceed a translation
            translator_comments = []
            value = token.value[2:-2].strip()
            for comment_tag in comment_tags:
                if value.startswith(comment_tag):
                    lines = value.splitlines()
                    if lines:
                        lines[0] = lines[0].strip()
                        lines[1:] = dedent('\n'.join(lines[1:])).splitlines()
                        for offset, line in enumerate(lines):
                            translator_comments.append((token.lineno + offset,
                                                        line))
                    break

        elif funcname and call_stack == 0:
            if token.type == 'operator' and token.value == ')':
                if last_argument is not None:
                    messages.append(last_argument)
                if len(messages) > 1:
                    messages = tuple(messages)
                elif messages:
                    messages = messages[0]
                else:
                    messages = None

                # Comments don't apply unless they immediately precede the
                # message
                if translator_comments and \
                   translator_comments[-1][0] < message_lineno - 1:
                    translator_comments = []

                if messages is not None:
                    yield (message_lineno, funcname, messages,
                           [comment[1] for comment in translator_comments])

                funcname = message_lineno = last_argument = None
                concatenate_next = False
                translator_comments = []
                messages = []
                call_stack = -1

            elif token.type == 'string':
                new_value = unquote_string(token.value)
                if concatenate_next:
                    last_argument = (last_argument or '') + new_value
                    concatenate_next = False
                else:
                    last_argument = new_value

            elif token.type == 'operator':
                if token.value == ',':
                    if last_argument is not None:
                        messages.append(last_argument)
                        last_argument = None
                    else:
                        messages.append(None)
                    concatenate_next = False
                elif token.value == '+':
                    concatenate_next = True

        elif call_stack > 0 and token.type == 'operator' \
             and token.value == ')':
            call_stack -= 1

        elif funcname and call_stack == -1:
            funcname = None

        elif call_stack == -1 and token.type == 'name' and \
             token.value in keywords and \
             (last_token is None or last_token.type != 'name' or
              last_token.value != 'function'):
            funcname = token.value

        last_token = token
        


src/r/e/reddit-HEAD/r2/r2/lib/translation.py   reddit(Download)
import tokenize
 
from babel.messages.extract import extract_javascript
from cStringIO import StringIO
 
def extract_javascript_msgids(source):
    """Return message ids of translateable strings in JS source."""
 
    extracted = extract_javascript(
        fileobj=StringIO(source),

src/b/l/bloodhound-HEAD/trac/trac/dist.py   bloodhound(Download)
try:
    from babel.messages.catalog import TranslationError
    from babel.messages.extract import extract_javascript
    from babel.messages.frontend import extract_messages, init_catalog, \
                                        compile_catalog, update_catalog
        stream.render(out=out, encoding='utf-8')
        out.seek(0)
        return extract_javascript(out, keywords, comment_tags, options)
 
 

src/t/r/Trac-1.0.1/trac/dist.py   Trac(Download)
try:
    from babel.messages.catalog import TranslationError
    from babel.messages.extract import extract_javascript
    from babel.messages.frontend import extract_messages, init_catalog, \
                                        compile_catalog, update_catalog
        stream.render(out=out, encoding='utf-8')
        out.seek(0)
        return extract_javascript(out, keywords, comment_tags, options)
 
 

src/t/r/trac-HEAD/trac/dist.py   trac(Download)
try:
    from babel.messages.catalog import TranslationError
    from babel.messages.extract import extract_javascript
    from babel.messages.frontend import extract_messages, init_catalog, \
                                        compile_catalog, update_catalog
        stream.render(out=out, encoding='utf-8')
        out.seek(0)
        return extract_javascript(out, keywords, comment_tags, options)
 
 

src/h/u/hue-HEAD/desktop/core/ext-py/Babel-0.9.6/babel/messages/tests/extract.py   hue(Download)
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

src/h/o/hortonworks-sandbox-HEAD/desktop/core/ext-py/Babel-0.9.6/babel/messages/tests/extract.py   hortonworks-sandbox(Download)
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

src/k/i/kitsune-HEAD/vendor/packages/Babel/babel/messages/tests/extract.py   kitsune(Download)
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

src/f/j/fjord-HEAD/vendor/packages/Babel/tests/messages/test_extract.py   fjord(Download)
msg = _('Bonjour à tous')
""".encode('utf-8'))
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""".encode('utf-8'))
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

src/g/a/gae-boilerplate-HEAD/bp_includes/external/babel/messages/tests/extract.py   gae-boilerplate(Download)
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

src/w/e/webapp-improved-HEAD/lib/babel/babel/messages/tests/extract.py   webapp-improved(Download)
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello'], messages[0][3])
msg = _('Bonjour à tous')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Bonjour à tous', messages[0][2])
        self.assertEqual([u'NOTE: hello', 'and bonjour', '  and servus'], messages[0][3])
}""")
 
        messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
        self.assertEqual(messages, [])
 
_('no comment here')
""")
        messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
        self.assertEqual(u'Something', messages[0][2])
        self.assertEqual([u'NOTE: this will'], messages[0][3])

  1 | 2 | 3  Next