Skip to content

Commit

Permalink
JavaScript: support tags as keywords for template strings
Browse files Browse the repository at this point in the history
Refs #329
  • Loading branch information
akx committed Feb 7, 2016
1 parent f061cc1 commit 5b09b64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def extract_javascript(fileobj, keywords, comment_tags, options):
* `template_string` -- set to false to disable ES6
template string support.
"""
from babel.messages.jslexer import tokenize, unquote_string
from babel.messages.jslexer import Token, tokenize, unquote_string
funcname = message_lineno = None
messages = []
last_argument = None
Expand All @@ -528,6 +528,16 @@ def extract_javascript(fileobj, keywords, comment_tags, options):
template_string=options.get("template_string", True),
dotted=dotted
):
if ( # Turn keyword`foo` expressions into keyword("foo") calls:
funcname and # have a keyword...
(last_token and last_token.type == 'name') and # we've seen nothing after the keyword...
token.type == 'template_string' # this is a template string
):
message_lineno = token.lineno
messages = [unquote_string(token.value)]
call_stack = 0
token = Token('operator', ')', token.lineno)

if token.type == 'operator' and token.value == '(':
if funcname:
message_lineno = token.lineno
Expand Down
9 changes: 9 additions & 0 deletions tests/messages/test_js_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ def test_template_string_standard_usage():
)

assert messages == [(1, 'Very template, wow', [], None)]


def test_template_string_tag_usage():
buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }")
messages = list(
extract.extract('javascript', buf, {"i18n": None}, [], {})
)

assert messages == [(1, 'Tag template, wow', [], None)]

0 comments on commit 5b09b64

Please sign in to comment.