From f87b58fcabf84c9b73b07b587416cc0b6cc814b1 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Thu, 16 Jun 2016 05:06:32 +0900 Subject: [PATCH] i18n: trim whitespaces in extracted messages from AngularJS templates When extracting translation messages from AngularJS templates, there is no need to keep whitespaces in the messages as there is no meaning of repeated whitespaces in HTML. This will make translation effort much simpler. More detail is described in the bug report. This commit trims such whitespaces. Django provides a convenient method to do the same purpose for 'trimmed' option in Django templates. This method is reused in this commit as well. Closes-Bug: #1592965 Change-Id: I9b7ce54452f3db2350eecc3115db2e4173df5167 --- horizon/test/tests/babel_extract_angular.py | 2 +- horizon/utils/babel_extract_angular.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/horizon/test/tests/babel_extract_angular.py b/horizon/test/tests/babel_extract_angular.py index 4c712b96e..68ea0637d 100644 --- a/horizon/test/tests/babel_extract_angular.py +++ b/horizon/test/tests/babel_extract_angular.py @@ -159,7 +159,7 @@ class ExtractAngularTestCase(test.TestCase): "") messages = list(extract_angular(buf, [], [], {})) - self.assertEqual([(1, 'gettext', 'hello\n world!', [])], messages) + self.assertEqual([(1, 'gettext', 'hello world!', [])], messages) def test_nested_translate_tag(self): buf = StringIO( diff --git a/horizon/utils/babel_extract_angular.py b/horizon/utils/babel_extract_angular.py index 91c1eae21..d98b9ccb7 100644 --- a/horizon/utils/babel_extract_angular.py +++ b/horizon/utils/babel_extract_angular.py @@ -15,6 +15,7 @@ import re +from django.utils.translation import trim_whitespace from six.moves import html_parser @@ -126,12 +127,12 @@ class AngularGettextHTMLParser(html_parser.HTMLParser): return if self.plural_form: messages = ( - self.data.strip(), - self.plural_form + trim_whitespace(self.data.strip()), + trim_whitespace(self.plural_form) ) func_name = u'ngettext' else: - messages = self.data.strip() + messages = trim_whitespace(self.data.strip()) func_name = u'gettext' self.strings.append( (self.line, func_name, messages, self.comments)