Add lingua plugin
diff --git a/mako/ext/linguaplugin.py b/mako/ext/linguaplugin.py
new file mode 100644
index 0000000..3bbdb2f
--- /dev/null
+++ b/mako/ext/linguaplugin.py
@@ -0,0 +1,27 @@
+from lingua.extractors import Extractor
+from lingua.extractors.python import _extract_python
+from mako.ext.extract import MessageExtractor
+
+
+class LinguaMakoExtractor(Extractor, MessageExtractor):
+ '''Mako templates'''
+ extensions = ['.mako']
+ default_config = {
+ 'encoding': 'utf-8',
+ }
+
+ def __call__(self, filename, options):
+ self.options = options
+ self.filename = filename
+ with open(filename) as input:
+ return self.process_file(fileobj)
+
+ def process_python(self, code, code_lineno, translator_strings):
+ for msg in _extract_python(self.filename, code.getvalue(), self.options,
+ code_lineno):
+ if translator_strings:
+ msg = Message(msg.msgctxt, msg.msgid, msg.msgid_plural,
+ msg.flags,
+ u' '.join(translator_strings + [msg.comment]),
+ msg.tcomment, msg.location)
+ yield msg
diff --git a/setup.py b/setup.py
index a62a301..93f861a 100644
--- a/setup.py
+++ b/setup.py
@@ -66,6 +66,9 @@
[babel.extractors]
mako = mako.ext.babelplugin:extract
+ [lingua.extractors]
+ mako = mako.ext.linguaplugin:LinguaMakoExtractor
+
[console_scripts]
mako-render = mako.cmd:cmdline
"""
diff --git a/test/ext/test_linguaplugin.py b/test/ext/test_linguaplugin.py
new file mode 100644
index 0000000..841f206
--- /dev/null
+++ b/test/ext/test_linguaplugin.py
@@ -0,0 +1,19 @@
+import io
+import mock
+import unittest
+from mako.ext.linguaplugin import LinguaMakoExtractor
+
+
+class MockOptions:
+ keywords = []
+ domain = None
+
+
+class Test_LinguaMakoExtractor(unittest.TestCase):
+ def test_parse_python_expression(self):
+ plugin = LinguaMakoExtractor()
+ plugin.options = MockOptions()
+ plugin.filename = 'dummy.mako'
+ input = io.BytesIO(b'<p>${_("Message")}</p>')
+ messages = list(plugin.process_file(input))
+ self.assertEqual(len(messages), 1)