# HG changeset patch # User cmlenz # Date 1187046290 0 # Node ID 6d51c8df5d5a2067cfcfdb0a6bf4daa91723f75a # Parent a3c011711a3063b2bfcac2b5eb0da69abc7660e6 Ported [708] to 0.4.x branch. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,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 diff --git a/doc/i18n.txt b/doc/i18n.txt --- 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 =========== diff --git a/genshi/filters/i18n.py b/genshi/filters/i18n.py --- 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): """Translate any localizable strings in the given stream. @@ -126,6 +130,8 @@ ignore_tags = self.ignore_tags include_attrs = self.include_attrs translate = self.translate + if not self.extract_text: + search_text = False skip = 0 xml_lang = XML_NAMESPACE['lang'] @@ -153,7 +159,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: @@ -232,6 +238,8 @@ yielded, instead an item for each string argument. """ tagname = None + if not self.extract_text: + search_text = False skip = 0 xml_lang = XML_NAMESPACE['lang'] @@ -251,7 +259,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: @@ -305,6 +313,7 @@ yield lineno, funcname, text + def extract(fileobj, keywords, comment_tags, options): """Babel extraction method for Genshi templates. diff --git a/genshi/filters/tests/i18n.py b/genshi/filters/tests/i18n.py --- a/genshi/filters/tests/i18n.py +++ b/genshi/filters/tests/i18n.py @@ -21,6 +21,16 @@ class TranslatorTestCase(unittest.TestCase): + def test_extract_without_text(self): + tmpl = MarkupTemplate(""" +

Foo

+ ${ngettext("Singular", "Plural", num)} + """) + translator = Translator(extract_text=False) + messages = list(translator.extract(tmpl.stream)) + self.assertEqual(1, len(messages)) + self.assertEqual((3, 'ngettext', (u'Singular', u'Plural')), messages[0]) + def test_extract_plural_form(self): tmpl = MarkupTemplate(""" ${ngettext("Singular", "Plural", num)}