本文整理汇总了Python中babel.messages.extract.extract_javascript函数的典型用法代码示例。如果您正苦于以下问题:Python extract_javascript函数的具体用法?Python extract_javascript怎么用?Python extract_javascript使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了extract_javascript函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_misplaced_comments
def test_misplaced_comments(self):
buf = BytesIO(
b"""\
/* NOTE: this won't show up */
foo()
/* NOTE: this will */
msg = _('Something')
// NOTE: this will show up
// too.
msg = _('Something else')
// NOTE: but this won't
bar()
_('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])
self.assertEqual(u"Something else", messages[1][2])
self.assertEqual([u"NOTE: this will show up", "too."], messages[1][3])
self.assertEqual(u"no comment here", messages[2][2])
self.assertEqual([], messages[2][3])
示例2: test_jsx_extraction
def test_jsx_extraction(jsx_enabled):
buf = BytesIO(JSX_SOURCE)
messages = [m[2] for m in extract.extract_javascript(buf, ('_', 'gettext'), [], {"jsx": jsx_enabled})]
if jsx_enabled:
assert messages == EXPECTED_JSX_MESSAGES
else:
assert messages != EXPECTED_JSX_MESSAGES
示例3: test_message_with_line_comment
def test_message_with_line_comment(self):
buf = BytesIO(u"""\
// NOTE: hello
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])
示例4: test_message_with_line_comment
def test_message_with_line_comment():
buf = BytesIO(u"""\
// NOTE: hello
msg = _('Bonjour à tous')
""".encode('utf-8'))
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
assert messages[0][2] == u'Bonjour à tous'
assert messages[0][3] == [u'NOTE: hello']
示例5: test_ignore_function_definitions
def test_ignore_function_definitions(self):
buf = StringIO("""\
function gettext(value) {
return translations[language][value] || value;
}""")
messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
self.assertEqual(messages, [])
示例6: extract_javascript_msgids
def extract_javascript_msgids(source):
"""Return message ids of translateable strings in JS source."""
extracted = extract_javascript(
fileobj=StringIO(source), keywords={"_": None, "P_": (1, 2), "N_": None}, comment_tags={}, options={}
)
return [msg_id for line, func, msg_id, comments in extracted]
示例7: test_ignore_function_definitions
def test_ignore_function_definitions():
buf = BytesIO(b"""\
function gettext(value) {
return translations[language][value] || value;
}""")
messages = list(extract.extract_javascript(buf, ('gettext',), [], {}))
assert not messages
示例8: test_message_with_line_comment
def test_message_with_line_comment(self):
buf = StringIO("""\
// NOTE: hello
msg = _('Bonjour à tous')
""")
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
self.assertEqual('Bonjour \xe0 tous', messages[0][2])
self.assertEqual(['NOTE: hello'], messages[0][3])
示例9: test_message_with_multiple_line_comments
def test_message_with_multiple_line_comments(self):
buf = StringIO("""\
// NOTE: hello
// goodbye
msg = _('Bonjour à tous')
""")
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Bonjour à tous', messages[0][2])
self.assertEqual([u'NOTE: hello goodbye'], messages[0][3])
示例10: test_message_with_multiline_comment
def test_message_with_multiline_comment(self):
buf = StringIO("""\
/* NOTE: hello
and bonjour
and servus */
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])
示例11: test_message_with_line_comment
def test_message_with_line_comment(self):
buf = BytesIO(
u"""\
// NOTE: hello
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])
示例12: extract_javascript_script
def extract_javascript_script(fileobj, keywords, comment_tags, options):
"""Extract messages from Javascript embedding in <script> tags.
Select <script type="javascript/text"> tags and delegate to
`extract_javascript`.
"""
from genshi.core import Stream
from genshi.input import XMLParser
out = StringIO()
stream = Stream(XMLParser(fileobj))
stream.select('//script[@type="text/javascript"]').render(out=out)
out.seek(0)
return extract_javascript(out, keywords, comment_tags, options)
示例13: extract_mxml
def extract_mxml(fileobj, keywords, comment_tags, options):
for elem, pos in MXMLParser(fileobj).parse():
if elem.tag == 'mx:Script':
for lineno, funcname, message, comments in \
extract_javascript(StringIO(elem.text), keywords,
comment_tags, options):
yield pos[0]+lineno, funcname, message, comments
else:
attrib = None
for attr in options.get('attrs', []):
if elem.get(attr):
attrib = attr
if attrib:
if elem.attrib[attrib].startswith('{') and \
elem.attrib[attrib].endswith('}'):
for _, funcname, message, comments in extract_actionscript(
StringIO(elem.attrib[attrib][1:-1]),
keywords, comment_tags, options):
yield pos[0], funcname, message, comments
示例14: extract_javascript
def extract_javascript(fileobj, keywords, comment_tags, options):
"""Extract messages from Javascript source files. This extractor delegates
to babel's buit-in javascript extractor, but adds a special comment
used as a flag to identify web translations.
:param fileobj: the 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)
:return: an iterator over ``(lineno, funcname, message, comments)``
tuples
:rtype: ``iterator``
"""
for (message_lineno, funcname, messages, comments) in \
extract.extract_javascript(fileobj, keywords, comment_tags, options):
comments.append(TRANSLATION_FLAG_COMMENT)
yield (message_lineno, funcname, messages, comments)
示例15: extract_mxml
def extract_mxml(fileobj, keywords, comment_tags, options):
attrs = set(options.get('attrs', [])).union(
set([u'label', u'text', u'title', u'headerText', u'prompt']))
encoding = options.get('encoding', 'utf-8')
for elem, pos in MXMLParser(fileobj).parse():
if elem.tag == 'mx:Script':
for lineno, funcname, message, comments in \
extract_javascript(StringIO(elem.text), keywords,
comment_tags, options):
yield pos[0]+lineno, funcname, message, comments
else:
attrib = None
for attr in attrs:
if elem.get(attr):
attrib = attr
if attrib:
if elem.attrib[attrib].startswith('{') and \
elem.attrib[attrib].endswith('}'):
contents = elem.attrib[attrib][1:-1].encode(encoding)
for _, funcname, message, comments in extract_actionscript(
StringIO(contents), keywords, comment_tags, options):
yield pos[0], funcname, message, comments