diff genshi/filters/i18n.py @ 594:2bbaa61b328f trunk

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 b00765a115a5
children badb5e5b7bb9
line wrap: on
line diff
--- 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.
     
Copyright (C) 2012-2017 Edgewall Software