Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Extract ``_`` calls from xml #7

Merged
merged 1 commit into from

2 participants

@teixas

Extract _ calls from xml templates inside tag attributes as well as in ${} expressions.

@wichert wichert merged commit 02df5c2 into wichert:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 4, 2012
  1. @teixas

    Extract ``_`` calls from xml

    teixas authored
This page is out of date. Refresh to see the latest.
View
83 src/lingua/extractors/tests/test_xml.py
@@ -201,3 +201,86 @@ def test_ignore_dynamic_message(self):
</html>
"""
self.assertEqual(self.extract(snippet), [])
+
+ def test_translate_underscore_call_with_unicode_arg(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy tal:replace="_(u'foo')">Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", [])])
+
+ def test_translate_underscore_call_with_str_arg(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy tal:replace='_("foo")'>Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", [])])
+
+ def test_translate_underscore_call_with_parenthesis(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy tal:replace='_("f (o) o")'>Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"f (o) o", [])])
+
+ def test_translate_underscore_call_with_default(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy tal:replace='_("foo", default="blah")'>Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", [u'Default: blah'])])
+
+ def test_translate_multiple_underscore_calls(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy \
+ tal:replace="multiple(_('foo'), _('blah'))">Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", []),
+ (3, None, u"blah", [])])
+
+ def test_translate_inner_underscore_call(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy>${_("foo")}</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", [])])
+
+ def test_translate_inner_multiple_underscore_calls(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy>${dummy(_("foo"), _("bar"))}</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", []),
+ (3, None, u"bar", [])])
+
+ def test_translate_inner_underscore_call_with_default(self):
+ snippet = """\
+ <html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+ i18n:domain="lingua">
+ <dummy>${_("foo", default="blah")}>Dummy</dummy>
+ </html>
+ """
+ self.assertEqual(self.extract(snippet),
+ [(3, None, u"foo", [u'Default: blah'])])
View
20 src/lingua/extractors/xml.py
@@ -1,8 +1,11 @@
from __future__ import absolute_import
import collections
import re
+from StringIO import StringIO
from xml.parsers import expat
+from lingua.extractors.python import PythonExtractor
+
class TranslateContext(object):
WHITESPACE = re.compile(u"\s{2,}")
@@ -41,6 +44,7 @@ def message(self):
class XmlExtractor(object):
ENTITY = re.compile(r"&([A-Za-z]+|#[0-9]+);")
+ UNDERSCORE_CALL = re.compile("_\(")
def __call__(self, fileobj, keywords, comment_tags, options):
self.keywords = keywords
@@ -70,6 +74,15 @@ def addMessage(self, message, comments=[]):
self.messages.append(
(self.parser.CurrentLineNumber, None, message, comments))
+ def addUndercoreCalls(self, message):
+ msg = message
+ if isinstance(msg, unicode):
+ msg = msg.encode('utf-8')
+ py_extractor = PythonExtractor()
+ py_messages = py_extractor(StringIO(msg), ['_'], None, None)
+ for (line, _, py_message, comments) in py_messages:
+ self.addMessage(py_message, comments)
+
def StartElementHandler(self, name, attributes):
i18n_prefix = self.prefix_stack[-1]
for (attr, value) in attributes.items():
@@ -114,11 +127,18 @@ def StartElementHandler(self, name, attributes):
continue
self.addMessage(msgid, [u'Default: %s' % attributes[attr]])
+ for (attr, value) in attributes.items():
+ if self.UNDERSCORE_CALL.search(value):
+ self.addUndercoreCalls(value)
+
def DefaultHandler(self, data):
if data.startswith(u'&') and self.translatestack[-1]:
self.translatestack[-1].addText(data)
def CharacterDataHandler(self, data):
+ if TranslateContext.EXPRESSION.search(data) and \
+ self.UNDERSCORE_CALL.search(data):
+ self.addUndercoreCalls(data)
if not self.translatestack[-1]:
return
Something went wrong with that request. Please try again.