Mercurial > genshi > mirror
changeset 595:66fd7f370acb stable-0.4.x
Ported [708] to 0.4.x branch.
author | cmlenz |
---|---|
date | Mon, 13 Aug 2007 23:04:50 +0000 |
parents | bb8141898891 |
children | 19345798ba5e |
files | ChangeLog doc/i18n.txt genshi/filters/i18n.py genshi/filters/tests/i18n.py |
diffstat | 4 files changed, 39 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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): """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.
--- 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("""<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')), messages[0]) + def test_extract_plural_form(self): tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> ${ngettext("Singular", "Plural", num)}