changeset 486:6a48853a2e36 stable-0.4.x

Ported [585] to 0.4.x.
author cmlenz
date Mon, 21 May 2007 08:31:48 +0000
parents f38f1b307160
children 56daac6d675f
files genshi/filters/i18n.py genshi/filters/tests/i18n.py
diffstat 2 files changed, 56 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/filters/i18n.py
+++ b/genshi/filters/i18n.py
@@ -149,7 +149,7 @@
 
                 yield kind, (tag, attrs), pos
 
-            elif kind is TEXT:
+            elif search_text and kind is TEXT:
                 text = data.strip()
                 if text:
                     data = data.replace(text, translate(text))
@@ -166,7 +166,8 @@
     GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext', 'dgettext', 'dngettext',
                          'ugettext', 'ungettext')
 
-    def extract(self, stream, gettext_functions=GETTEXT_FUNCTIONS):
+    def extract(self, stream, gettext_functions=GETTEXT_FUNCTIONS,
+                search_text=True):
         """Extract localizable strings from the given template stream.
         
         For every string found, this function yields a ``(lineno, function,
@@ -203,6 +204,8 @@
         :param gettext_functions: a sequence of function names that should be
                                   treated as gettext-style localization
                                   functions
+        :param search_text: whether the content of text nodes should be
+                            extracted (used internally)
         
         :note: Changed in 0.4.1: For a function with multiple string arguments
                (such as ``ngettext``), a single item with a tuple of strings is
@@ -237,10 +240,11 @@
                                 yield pos[1], None, text
                     else:
                         for lineno, funcname, text in self.extract(
-                                _ensure(value), gettext_functions):
+                                _ensure(value), gettext_functions,
+                                search_text=name in self.include_attrs):
                             yield lineno, funcname, text
 
-            elif kind is TEXT:
+            elif search_text and kind is TEXT:
                 text = data.strip()
                 if text and filter(None, [ch.isalpha() for ch in text]):
                     yield pos[1], None, text
--- a/genshi/filters/tests/i18n.py
+++ b/genshi/filters/tests/i18n.py
@@ -12,17 +12,64 @@
 # history and logs, available at http://genshi.edgewall.org/log/.
 
 import doctest
+from StringIO import StringIO
 import unittest
 
+from genshi.template import MarkupTemplate
 from genshi.filters.i18n import Translator
 
 
 class TranslatorTestCase(unittest.TestCase):
-    pass
+
+    def test_extract_plural_form(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          ${ngettext("Singular", "Plural", num)}
+        </html>""")
+        translator = Translator()
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((2, 'ngettext', (u'Singular', u'Plural')), messages[0])
+
+    def test_extract_included_attribute_text(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          <span title="Foo"></span>
+        </html>""")
+        translator = Translator()
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((2, None, u'Foo'), messages[0])
+
+    def test_extract_attribute_expr(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          <input type="submit" value="${_('Save')}" />
+        </html>""")
+        translator = Translator()
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((2, '_', u'Save'), messages[0])
+
+    def test_extract_non_included_attribute_interpolated(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          <a href="#anchor_${num}">Foo</a>
+        </html>""")
+        translator = Translator()
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((2, None, u'Foo'), messages[0])
+
+    def test_extract_text_from_sub(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          <py:if test="foo">Foo</py:if>
+        </html>""")
+        translator = Translator()
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((2, None, u'Foo'), messages[0])
 
 
 def suite():
     suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
     suite.addTests(doctest.DocTestSuite(Translator.__module__))
     return suite
 
Copyright (C) 2012-2017 Edgewall Software