changeset 594:0fb43dc2e165

Add option to I18n filter to only extract strings in gettext function calls.
author cmlenz
date Mon, 13 Aug 2007 23:02:46 +0000
parents aa5762c7b7f1
children f436c7db99f5
files ChangeLog doc/i18n.txt genshi/filters/i18n.py genshi/filters/tests/i18n.py
diffstat 4 files changed, 40 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,8 @@
  * The template loader now raises a `TemplateNotFound` error when a previously
    cached template is removed or renamed, where it previously was passing up
    an `OSError`.
+ * The Genshi I18n filter can be configured to only extract messages found in
+   `gettext` function calls, ignoring any text nodes and attribute values.
 
 
 Version 0.4.3
--- a/doc/i18n.txt
+++ b/doc/i18n.txt
@@ -145,20 +145,31 @@
 .. _`text templates`: text-templates.html
 
 ``encoding``
-------------------
+------------
 The encoding of the template file. This is only used for text templates. The
 default is to assume “utf-8”.
 
 ``include_attrs``
-------------------
+-----------------
 Comma-separated list of attribute names that should be considered to have 
 localizable values. Only used for markup templates.
 
-``include_tags``
-------------------
+``ignore_tags``
+---------------
 Comma-separated list of tag names that should be ignored. Only used for markup 
 templates.
 
+``extract_text``
+----------------
+Whether text outside explicit ``gettext`` function calls should be extracted.
+By default, any text nodes not inside ignored tags, and values of attribute in
+the ``include_attrs`` list are extracted. If this option is disabled, only 
+strings in ``gettext`` function calls are extracted.
+
+.. note:: If you disable this option, it's not necessary to add the translation
+          filter as described above. You only need to make sure that the
+          template has access to the ``gettext`` functions it uses.
+
 
 Translation
 ===========
--- a/genshi/filters/i18n.py
+++ b/genshi/filters/i18n.py
@@ -96,17 +96,21 @@
                                'summary', 'title'])
 
     def __init__(self, translate=gettext, ignore_tags=IGNORE_TAGS,
-                 include_attrs=INCLUDE_ATTRS):
+                 include_attrs=INCLUDE_ATTRS, extract_text=True):
         """Initialize the translator.
         
         :param translate: the translation function, for example ``gettext`` or
                           ``ugettext``.
         :param ignore_tags: a set of tag names that should not be localized
         :param include_attrs: a set of attribute names should be localized
+        :param extract_text: whether the content of text nodes should be
+                             extracted, or only text in explicit ``gettext``
+                             function calls
         """
         self.translate = translate
         self.ignore_tags = ignore_tags
         self.include_attrs = include_attrs
+        self.extract_text = extract_text
 
     def __call__(self, stream, ctxt=None, search_text=True, msgbuf=None):
         """Translate any localizable strings in the given stream.
@@ -127,6 +131,8 @@
         ignore_tags = self.ignore_tags
         include_attrs = self.include_attrs
         translate = self.translate
+        if not self.extract_text:
+            search_text = False
         skip = 0
         i18n_msg = I18N_NAMESPACE['msg']
         ns_prefixes = []
@@ -156,7 +162,7 @@
                 changed = False
                 for name, value in attrs:
                     newval = value
-                    if isinstance(value, basestring):
+                    if search_text and isinstance(value, basestring):
                         if name in include_attrs:
                             newval = self.translate(value)
                     else:
@@ -258,6 +264,8 @@
                (such as ``ngettext``), a single item with a tuple of strings is
                yielded, instead an item for each string argument.
         """
+        if not self.extract_text:
+            search_text = False
         skip = 0
         i18n_msg = I18N_NAMESPACE['msg']
         xml_lang = XML_NAMESPACE['lang']
@@ -279,7 +287,7 @@
                     continue
 
                 for name, value in attrs:
-                    if isinstance(value, basestring):
+                    if search_text and isinstance(value, basestring):
                         if name in self.include_attrs:
                             text = value.strip()
                             if text:
@@ -455,6 +463,7 @@
 
     return parts
 
+
 def extract(fileobj, keywords, comment_tags, options):
     """Babel extraction method for Genshi templates.
     
--- a/genshi/filters/tests/i18n.py
+++ b/genshi/filters/tests/i18n.py
@@ -21,6 +21,17 @@
 
 class TranslatorTestCase(unittest.TestCase):
 
+    def test_extract_without_text(self):
+        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
+          <p title="Bar">Foo</p>
+          ${ngettext("Singular", "Plural", num)}
+        </html>""")
+        translator = Translator(extract_text=False)
+        messages = list(translator.extract(tmpl.stream))
+        self.assertEqual(1, len(messages))
+        self.assertEqual((3, 'ngettext', (u'Singular', u'Plural', None)),
+                         messages[0])
+
     def test_extract_plural_form(self):
         tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
           ${ngettext("Singular", "Plural", num)}
Copyright (C) 2012-2017 Edgewall Software