Complete angular translation extract pattern

Currently AngularGettextHTMLParser parses translations of form
{$ 'content' | translate $}. However, there is another form in
projects' html files: {$ ::'content' | translate $}.

This commit fine-tunes the filter_regex and related handle_xxx
functions to extract both forms of translations.

Change-Id: I7129a13d046b699328b77267ae4936d31af8144c
This commit is contained in:
francotseng 2019-04-03 14:17:37 +08:00
parent d6cb98529a
commit 5de40b3f39
2 changed files with 21 additions and 3 deletions

View File

@ -135,6 +135,15 @@ class ExtractAngularTestCase(test.TestCase):
<img alt="{$'some other thing'$}">
<p>{$'"it\\'s awesome"'|translate$}</p>
<p>{$"oh \\"hello\\" there"|translate$}</p>
<img alt="{$::'hello colon1' | translate $}">
<p>{$ ::'hello colon2' |translate$}</p>
<p>{$ :: 'hello colon3'| translate$}</p>
<img alt="something {$::'hello colon4'|translate$} something
{$ ::'hello colon5' | translate$}">
<img alt="{::$expr()|translate$}">
<img alt="{$::'some other thing'$}">
<p>{$:: '"it\\'s awesome"'|translate$}</p>
<p>{$ :: "oh \\"hello\\" there" | translate$}</p>
"""
)
@ -147,6 +156,13 @@ class ExtractAngularTestCase(test.TestCase):
(4, u'gettext', 'hello world4', []),
(8, u'gettext', '"it\\\'s awesome"', []),
(9, u'gettext', 'oh \\"hello\\" there', []),
(10, u'gettext', u'hello colon1', []),
(11, u'gettext', u'hello colon2', []),
(12, u'gettext', u'hello colon3', []),
(13, u'gettext', u'hello colon4', []),
(13, u'gettext', u'hello colon5', []),
(17, u'gettext', u'"it\\\'s awesome"', []),
(18, u'gettext', u'oh \\"hello\\" there', []),
],
messages)

View File

@ -21,7 +21,7 @@ from six.moves import html_parser
# regex to find filter translation expressions
filter_regex = re.compile(
r"""{\$\s*('([^']|\\')+'|"([^"]|\\")+")\s*\|\s*translate\s*\$}"""
r"""{\$\s*(::)?\s*('([^']|\\')+'|"([^"]|\\")+")\s*\|\s*translate\s*\$}"""
)
# browser innerHTML decodes some html entities automatically, so when
@ -49,6 +49,8 @@ class AngularGettextHTMLParser(html_parser.HTMLParser):
{$ 'content' | translate $}
The string will be translated, minus expression handling (i.e. just
bare strings are allowed.)
{$ ::'content' | translate $}
The string will be translated. As above.
"""
def __init__(self):
@ -94,7 +96,7 @@ class AngularGettextHTMLParser(html_parser.HTMLParser):
for match in filter_regex.findall(attr[1]):
if match:
self.strings.append(
(self.line, u'gettext', match[0][1:-1], [])
(self.line, u'gettext', match[1][1:-1], [])
)
def handle_data(self, data):
@ -103,7 +105,7 @@ class AngularGettextHTMLParser(html_parser.HTMLParser):
else:
for match in filter_regex.findall(data):
self.strings.append(
(self.line, u'gettext', match[0][1:-1], [])
(self.line, u'gettext', match[1][1:-1], [])
)
def handle_entityref(self, name):