Mercurial > genshi > mirror
changeset 522:082535e5087c trunk
The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
author | cmlenz |
---|---|
date | Fri, 15 Jun 2007 20:17:56 +0000 |
parents | 203bd6a3c93d |
children | 7553760b58af |
files | ChangeLog genshi/filters/i18n.py genshi/filters/tests/i18n.py |
diffstat | 3 files changed, 36 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,9 @@ * `TextTemplate` can now handle unicode source (ticket #125). * A `<?python ?>` processing instruction containing trailing whitespace no longer causes a syntax error (ticket #127). + * The I18n filter now skips the content of elements that have an `xml:lang` + attribute with a fixed string value. Basically, `xml:lang` can now be used + as a flag to mark specific sections as not needing localization. Version 0.4.1
--- a/genshi/filters/i18n.py +++ b/genshi/filters/i18n.py @@ -8,7 +8,8 @@ from opcode import opmap import re -from genshi.core import Attrs, Namespace, QName, START, END, TEXT, _ensure +from genshi.core import Attrs, Namespace, QName, START, END, TEXT, \ + XML_NAMESPACE, _ensure from genshi.template.base import Template, EXPR, SUB from genshi.template.markup import EXEC @@ -68,6 +69,10 @@ <p>Hallo, Hans</p> </body> </html> + + Note that elements defining ``xml:lang`` attributes that do not contain + variable expressions are ignored by this filter. That can be used to + exclude specific parts of a template from being extracted and translated. """ IGNORE_TAGS = frozenset([ @@ -109,25 +114,24 @@ include_attrs = self.include_attrs translate = self.translate skip = 0 + xml_lang = XML_NAMESPACE['lang'] for kind, data, pos in stream: # skip chunks that should not be localized if skip: if kind is START: - tag, attrs = data - if tag in ignore_tags: - skip += 1 + skip += 1 elif kind is END: - if tag in ignore_tags: - skip -= 1 + skip -= 1 yield kind, data, pos continue # handle different events that can be localized if kind is START: tag, attrs = data - if tag in ignore_tags: + if tag in self.ignore_tags or \ + isinstance(attrs.get(xml_lang), basestring): skip += 1 yield kind, data, pos continue @@ -216,22 +220,20 @@ """ tagname = None skip = 0 + xml_lang = XML_NAMESPACE['lang'] for kind, data, pos in stream: if skip: if kind is START: - tag, attrs = data - if tag in self.ignore_tags: - skip += 1 + skip += 1 if kind is END: - tag = data - if tag in self.ignore_tags: - skip -= 1 + skip -= 1 continue if kind is START: tag, attrs = data - if tag in self.ignore_tags: + if tag in self.ignore_tags or \ + isinstance(attrs.get(xml_lang), basestring): skip += 1 continue
--- a/genshi/filters/tests/i18n.py +++ b/genshi/filters/tests/i18n.py @@ -66,6 +66,23 @@ self.assertEqual(1, len(messages)) self.assertEqual((2, None, u'Foo'), messages[0]) + def test_ignore_tag_with_fixed_xml_lang(self): + tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> + <p xml:lang="en">(c) 2007 Edgewall Software</p> + </html>""") + translator = Translator() + messages = list(translator.extract(tmpl.stream)) + self.assertEqual(0, len(messages)) + + def test_extract_tag_with_variable_xml_lang(self): + tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> + <p xml:lang="${lang}">(c) 2007 Edgewall Software</p> + </html>""") + translator = Translator() + messages = list(translator.extract(tmpl.stream)) + self.assertEqual(1, len(messages)) + self.assertEqual((2, None, u'(c) 2007 Edgewall Software'), messages[0]) + def suite(): suite = unittest.TestSuite()