From 5de40b3f397f812d5236a274b22515b18118015c Mon Sep 17 00:00:00 2001 From: francotseng Date: Wed, 3 Apr 2019 14:17:37 +0800 Subject: [PATCH] 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 --- .../unit/utils/test_babel_extract_angular.py | 16 ++++++++++++++++ horizon/utils/babel_extract_angular.py | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/horizon/test/unit/utils/test_babel_extract_angular.py b/horizon/test/unit/utils/test_babel_extract_angular.py index f09b62879e..7aacf8ad2a 100644 --- a/horizon/test/unit/utils/test_babel_extract_angular.py +++ b/horizon/test/unit/utils/test_babel_extract_angular.py @@ -135,6 +135,15 @@ class ExtractAngularTestCase(test.TestCase): {$'some other thing'$}

{$'"it\\'s awesome"'|translate$}

{$"oh \\"hello\\" there"|translate$}

+ {$::'hello colon1' | translate $} +

{$ ::'hello colon2' |translate$}

+

{$ :: 'hello colon3'| translate$}

+ something {$::'hello colon4'|translate$} something
+            {$ ::'hello colon5' | translate$} + {::$expr()|translate$} + {$::'some other thing'$} +

{$:: '"it\\'s awesome"'|translate$}

+

{$ :: "oh \\"hello\\" there" | translate$}

""" ) @@ -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) diff --git a/horizon/utils/babel_extract_angular.py b/horizon/utils/babel_extract_angular.py index cd9ba5e274..4d06c271ef 100644 --- a/horizon/utils/babel_extract_angular.py +++ b/horizon/utils/babel_extract_angular.py @@ -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):